Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » OpenLayer memory leak?

This thread is locked; no one can reply to it. rss feed Print
OpenLayer memory leak?
Richard Phipps
Member #1,632
November 2001
avatar

Maybe I'm missing something, but here is the upload_texture routine from OpenLayer:

1OlTextureInfo AllegroGLDriver::
2UploadTexture( OL_MEMORY_IMG *bmp, bool isSolid ) {
3 isSolid = false;
4 bool useTemp = false;
5
6 int bmpW = bmp->w;
7 int bmpH = bmp->h;
8
9 int textureW = bmpW;
10 int textureH = bmpH;
11
12 bool useExtendedDimensions = false;
13
14 if( !IsExtensionAlvailable( TEXTURE_NOT_POWER_OF_TWO_EXT )) {
15 int bmpW = bmp->w;
16 int bmpH = bmp->h;
17
18 textureW = ToNextPowOfTwo( bmpW );
19 textureH = ToNextPowOfTwo( bmpH );
20
21 useExtendedDimensions = textureW != bmpW || textureH != bmpH;
22 }
23
24 int format = isSolid? GL_RGB : GL_RGBA;
25 int bpp = isSolid? 3 : 4;
26 /*
27 unsigned char *data = new unsigned char[textureW * textureH * bpp];
28 unsigned char *pixelPtr = data;
29 */
30
31 unsigned char *data = new unsigned char[textureW * textureH * bpp];
32 //float *data = new float[textureW * textureH * bpp];
33
34 char buffer[100];
35
36 unsigned char *imageStart = data + bpp * (textureH - bmpH) * textureW;
37 unsigned char *pixelPtr = imageStart;
38 for( int y = bmpH-1; y >= 0; y-- ) {
39 for( int x = 0; x < bmpW; x++ ) {
40 int color = getpixel( bmp, x, y );
41 ExtractColor( pixelPtr, color, format );
42 }
43
44 pixelPtr += bpp * (textureW - bmpW);
45 }
46
47 if( useExtendedDimensions ) {
48 int lastLine = bmpH - 1;
49 pixelPtr = imageStart - bpp * textureW;
50 for( int x = 0; x < bmpW+1; x++ ) {
51 int color = getpixel( bmp, x, lastLine );
52 ExtractColor( pixelPtr, color, format );
53 }
54
55 for( int y = 0; y < bmpH; y++ ) {
56 int color = getpixel( bmp, bmpW-1, y );
57 pixelPtr = imageStart + bpp * (y * textureW + bmpW);
58 ExtractColor( pixelPtr, color, format );
59 }
60 }
61
62 OlTextureInfo textureInfo( textureW, textureH, bmp->w, bmp->h, format );
63
64 GLuint index = 0;
65 glGenTextures( 1, &index );
66 glBindTexture( GL_TEXTURE_2D, index );
67
68 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
69
70 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
71 GL_LINEAR_MIPMAP_NEAREST );
72
73 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
74
75 glTexImage2D( GL_TEXTURE_2D, 0, bpp, textureW, textureH, 0, format, GL_UNSIGNED_BYTE, data );
76
77 /*glTexImage2D( GL_TEXTURE_2D, 0, 4, textureInfo.texWidth, textureInfo.texHeight,
78 0, GL_RGBA, GL_UNSIGNED_BYTE, data );*/
79 textureInfo.SetIndex( index );
80
81 return textureInfo;
82}

Two things:

char buffer[100];

Is not used as far as I can see, but more importantly the malloced 'data' array is not freed at the end of the function. This data shouldn't still be needed in memory as this array is separate to the memory bitmap copy.

Unless I'm missing something?

orz
Member #565
August 2000

The buffer is on the stack, and therefore cannot be leaked (assuming the function returns sometime...).

The newed data array does appear to never be deleted. I believe that's a leak. At least, comparing it to other source code that calls glTexImage2D, like NeHe tutorial 06, I see that NeHe frees the memory it passes to glTexImage2D shortly after that call, whereas this code appears to let all pointers at the memory get lost.
edit: "points" -> "pointers" (typo)

Richard Phipps
Member #1,632
November 2001
avatar

Yep, buffer is on the stack, but it's not used so can be removed.

Yes, I imagine that the GlTexImage2D takes the data in the formats described and creates a version in video memory. So, the memory data passed can be freed.

Fladimir da Gorf
Member #1,565
October 2001
avatar

Thanks for noticing, I've got it fixed now :)

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Richard Phipps
Member #1,632
November 2001
avatar

Cool.

When can we see an official new release Fladimir? I think v2.0 is getting a bit old now. ;)

Epsi
Member #5,731
April 2005
avatar

As soon as his Rectangular TextRenderer Poly is working 8-)

;D

___________________________________

piccolo: "soon all new 2d alegro games will be better. after i finsh my MMRPG. my game will serve as a code reference. so you can understand and grab code from."
piccolo: "just wait until my invetion comes out its going to take the wii to the next leave of game play. it will run sony and microsoft out of busness if i dont let them use it aswell."

Richard Phipps
Member #1,632
November 2001
avatar

I'd like to see an new easy to compile version please. :)

Go to: