PDA

View Full Version : Memory Leak Loading Textures



01-26-2003, 07:49 AM
I've beeen doing some GUI stuff for my project and added the option to change the resolution and refresh rate in runtime.
It works fine, except the textures.

As i learned, shutting down the window (and the associated contexts ) "kills" my textures, so i have to reload them again after opengl gets enabled once more.

So far so good, except that for some reason, the aplication gets bigger everytime it reloads the textures.

The amount by which it gets bigger is exactly the file size of the textures..So its probably the loading procedures that don't delete the memory used in the process. I don't think its the case, but here is the code anyway:

unsigned int TexManager::LoadBmp( LPSTR strFileName,bool wrap)
{
unsigned int texID=0;
FILE * FilePointer;
AUX_RGBImageRec *pBitmap = NULL;

if(!strFileName)
{
//DB.write("Texture Manager - LoadBMP : No file Name Given\n");
return false;
}



FilePointer = fopen(strFileName, "r");
if(!FilePointer)
{
//DB.write("\n\nTexture Manager - ERROR - File \"%s\" Does Not Exist\n",strFileName);
}
else
fclose(FilePointer);

pBitmap = auxDIBImageLoad(strFileName);

if(pBitmap == NULL)
{

return false;
}

glGenTextures(1, &texID);


glBindTexture(GL_TEXTURE_2D, texID);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3 , pBitmap->sizeX, pBitmap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitmap->data);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);

if(!wrap)
{
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _CLAMP);
GLfloat border[4]={1.0,1.0,1.0,1.0};
glTexParameterfv(GL_TEXTURE_2D,GL_TEXTURE_BORDER_C OLOR,border); //necessário nas luzes de DOT 3 (fora da textura fica CLARO)
}

//mete prioridade máxima
GLclampf h=1.0;
glPrioritizeTextures(1,&texID,&h);


if (pBitmap)
{
if (pBitmap->data)
{
free (pBitmap->data);
}

free(pBitmap);
}


fclose(FilePointer);
return texID;
}

//tem memory leak de 8 kb's por load
unsigned int TexManager::LoadTGA( char * filename, bool wrap)
{
unsigned int texID=0;
GLubyte *imageData;
GLuint bpp;
GLuint width;
GLuint height;

GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
GLubyte TGAcompare[12];
GLubyte header[6];
GLuint bytesPerPixel;
GLuint imageSize;
GLuint temp;
GLuint type=GL_RGBA;

FILE *file = fopen(filename, "rb");
//ha erro nestas verificações
if( file==NULL | |
fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeo f(TGAcompare) | |
memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 | |
fread(header,1,sizeof(header),file)!=sizeof(header ))
{
if (file == NULL)
{
printf("FILE DOES NOT EXIST!!! \"%s\" \n",filename);
return false;
}
else
{

printf("FAILURE LOADING TGA FILE!!! Error Loading \"%s\" \n",filename);
fclose(file);

return false;
}
}

width = header[1] * 256 + header[0];
height = header[3] * 256 + header[2];

if( width <=0 &#0124; &#0124;
height <=0 &#0124; &#0124;
(header[4]!=24 && header[4]!=32))
{

printf("FAILURE LOADING TGA FILE!!! \"%s\" \n",filename);
fclose(file);

return false;
}

bpp = header[4];
bytesPerPixel = bpp/8;
imageSize = width*height*bytesPerPixel;

imageData=(GLubyte *)malloc(imageSize);

if( imageData==NULL &#0124; &#0124;
fread(imageData, 1, imageSize, file)!=imageSize)
{
if(imageData!=NULL)
free(imageData);

fclose(file);
return false;
}

for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)
{
temp=imageData[i];
imageData[i] = imageData[i + 2];
imageData[i + 2] = temp;
}

fclose (file);


glGenTextures(1, &texID);

glBindTexture(GL_TEXTURE_2D, texID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

if(!wrap)
{
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _CLAMP);
GLfloat border[4]={1.0,1.0,1.0,1.0};
glTexParameterfv(GL_TEXTURE_2D,GL_TEXTURE_BORDER_C OLOR,border); //necessário nas luzes de DOT 3 (fora da textura fica CLARO)
};

if (bpp==24)
{
type=GL_RGB;
}

gluBuild2DMipmaps(GL_TEXTURE_2D, type, width, height,(GLenum) type, GL_UNSIGNED_BYTE, imageData);

//mete prioridade máxima
GLclampf h=1.0;
glPrioritizeTextures(1,&texID,&h);


//liberta os dados - isto n estava antes
free(imageData);

return texID;
}

The problem is that using glDeleteTextures or not before killing the windows and context makes no diference whatsoever..In fact, after doing glDeleteTextures, some of them still appear as valid textures using glIsTexture...

If someone could help that would be most appreciated...

MorTyR