PDA

View Full Version : Memory leak in texture loading with loadImage using Devil



opty2001
08-03-2013, 09:12 AM
struct MOTION
{
int frame;
float interval;
unsigned int sprite;
float width;
float height;
};
struct MEM_TEST
{
vector<MOTION> x;
};

MEM_TEST memory;
void loadImage(OBJECTS::MOTION* target, const char* theFileName)
{
ILuint imageID;

GLuint textureID;

ILboolean success;

ILenum error;

ilGenImages(1, &imageID);

ilBindImage(imageID);

success = ilLoadImage(theFileName);

if (success)
{
ILinfo ImageInfo;
iluGetImageInfo(&ImageInfo);
if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
{
iluFlipImage();
}

success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

if (!success)
{
error = ilGetError();
exit(-1);
}

glGenTextures(1, &textureID);

glBindTexture(GL_TEXTURE_2D, textureID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D,
0,
ilGetInteger(IL_IMAGE_BPP),
ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT),
0,
ilGetInteger(IL_IMAGE_FORMAT),
GL_UNSIGNED_BYTE,
ilGetData());

}
else
{
Status=FALSE;
error = ilGetError();
exit(-1);
}

target->sprite = textureID;
target->width = ilGetInteger(IL_IMAGE_WIDTH);
target->height = ilGetInteger(IL_IMAGE_HEIGHT);

ilDeleteImages(1, &imageID);
}

LRESULT CALLBACK WndProc ... ...
{
switch (uMsg) ... ... case WM_KEYDOWN ... ... switch (wParam) ... ...

case 97 :
{
for(int i=0; i<10; i++)
{
memory.push_back(sample_memory);
}
return 0;
}
case 98 :
{
for(int i=0; i<memory.size(); i++)
{
for(int o=0; o<100; o++)
{
loadImage(&sample_motion,"sprite/backIMG/map0/far1/0.png");
memory[i].x.push_back(sample_motion);
}
}
return 0;
}

case 99 :
{
for(int i=0; i<memory.size(); i++)
{
memory[i].x.clear();
vector<MOTION>().swap(memory[i].x);
}
memory.clear();
vector<MEM_TEST>().swap(memory);
return 0;
}


I've tested this code, and result is..
1097
............

Of course, If i change case 98 to this,



case 98 :
{
loadImage(&sample_motion,"sprite/backIMG/map0/far1/0.png");
for(int i=0; i<memory.size(); i++)
{
for(int o=0; o<100; o++)
{
memory[i].x.push_back(sample_motion);
}
}
return 0;
}


then after I press 'numpad 3' (99), memory has been almost removed.
But in real code, I must use many 'loadImage' because images that used in game are all different.
How can I solve this memory leak problem?

Mike_R
08-04-2013, 04:48 AM
I would suggest to use special tools like Deleaker.

opty2001
08-04-2013, 08:39 PM
OK, thank you for your suggestion.