Sign in to follow this  
Sir Sapo

Freeing Texture Memory

Recommended Posts

Sir Sapo    769
Hey everyone, I've got a little problem here that I am having trouble solving, and I was hoping one of you guys could point me in the right direction. Here's the problem: I am loading all the textures into my game using the NeHe method he used in his tutorials. In my game, the textures are split up into tilesets, and which tileset is loaded depends on what environment the current level is. Now, the technique I am using right now works fine when I load the textures up at the beginning of my program, but when I call the function to load them again (except a different tileset), it uses up more RAM, and the old memory is not free'd. I don't understand how I would free the texture memory before loading the new textures, because the textures are stored as a non-pointer GLuint array. Is there a specific OGL function that will free texture memory? How should I tackle this problem? Thanks in Advance

Share this post

Link to post
Share on other sites
Sir Sapo    769
Thanks, but unfortunately, that isn't working for some reason. I tested it by looking at how much memory the textures took up without reloading them, and then I purposely loaded , deleted, then loaded them again and the memory usage still doubled. If it helps, here is my texture loading function:

int LoadGLTextures() // Load Bitmaps And Convert To Textures

tileset = 1;

int Status=FALSE; // Status Indicator
AUX_RGBImageRec *TextureImage[numtex]; // Create Storage Space For The Texture Data
memset(TextureImage,0,sizeof(void *)*numtex); // Set The Pointer To NULL

if ((TextureImage[0]=LoadBMP("Data/1/Ocean.bmp")) &&
(TextureImage[1]=LoadBMP("Data/1/BowHull.bmp")) &&
(TextureImage[2]=LoadBMP("Data/1/BoatHull.bmp")) &&
(TextureImage[3]=LoadBMP("Data/1/MidHull.bmp")) &&
(TextureImage[4]=LoadBMP("Data/1/SternHull.bmp")) &&
(TextureImage[5]=LoadBMP("Data/1/Ground.bmp")) &&
(TextureImage[6]=LoadBMP("Data/1/SuperStruct.bmp")) &&
(TextureImage[7]=LoadBMP("Data/1/Palmtree2.bmp")) &&
(TextureImage[8]=LoadBMP("Data/1/Palmtree3.bmp")) &&
(TextureImage[9]=LoadBMP("Data/1/Palmtree4.bmp")) &&
(TextureImage[10]=LoadBMP("Data/1/Runway.bmp")) &&
(TextureImage[11]=LoadBMP("Data/1/Geoform2.bmp")) &&
(TextureImage[12]=LoadBMP("Data/1/Geoform3.bmp")) &&
(TextureImage[13]=LoadBMP("Data/1/Geoform4.bmp")) &&
(TextureImage[14]=LoadBMP("Data/1/ShoreL.bmp")) &&
(TextureImage[15]=LoadBMP("Data/1/ShoreR.bmp")) &&
(TextureImage[16]=LoadBMP("Data/1/Hut1.bmp")) &&
(TextureImage[17]=LoadBMP("Data/1/Hut2.bmp")) &&
(TextureImage[18]=LoadBMP("Data/1/Hut3.bmp")) &&
(TextureImage[19]=LoadBMP("Data/1/711.bmp")) &&
(TextureImage[20]=LoadBMP("Data/1/Hospital.bmp")) &&
(TextureImage[21]=LoadBMP("Data/1/Building1.bmp")) &&
(TextureImage[22]=LoadBMP("Data/1/Building2.bmp")) &&
(TextureImage[23]=LoadBMP("Data/1/Building3.bmp")) &&
(TextureImage[24]=LoadBMP("Data/1/RockyTerrain1.bmp")) &&
(TextureImage[25]=LoadBMP("Data/1/RockyTerrain2.bmp")) &&
(TextureImage[26]=LoadBMP("Data/1/Crater1.bmp")) &&
(TextureImage[27]=LoadBMP("Data/1/Crater2.bmp")) &&
(TextureImage[28]=LoadBMP("Data/1/Crater3.bmp")) &&
(TextureImage[29]=LoadBMP("Data/1/EasterHead.bmp")) &&
(TextureImage[30]=LoadBMP("Data/1/CubaFlag.bmp")) &&
(TextureImage[31]=LoadBMP("Data/1/TropicalBG.bmp")) &&
(TextureImage[32]=LoadBMP("Data/1/NeoPartisanmask.bmp")) &&
(TextureImage[33]=LoadBMP("Data/1/NeoPartisan.bmp")) &&
(TextureImage[34]=LoadBMP("Data/1/HoverPartisan.bmp")) &&
(TextureImage[35]=LoadBMP("Data/1/HoverPartisanmask.bmp")) &&
(TextureImage[36]=LoadBMP("Data/1/NeoPartisan.bmp")) &&
(TextureImage[37]=LoadBMP("Data/1/NeoPartisanmask.bmp")) &&
(TextureImage[38]=LoadBMP("Data/1/Parachute.bmp")) &&
(TextureImage[39]=LoadBMP("Data/1/Parachutemask.bmp")) &&
(TextureImage[40]=LoadBMP("Data/1/Pilot.bmp")) &&
(TextureImage[41]=LoadBMP("Data/1/Pilotmask.bmp")) &&
(TextureImage[42]=LoadBMP("Data/1/Bomb.bmp")) &&
(TextureImage[43]=LoadBMP("Data/1/Bombmask.bmp")) &&
(TextureImage[44]=LoadBMP("Data/1/Rocket.bmp")) &&
(TextureImage[45]=LoadBMP("Data/1/Rocketmask.bmp")) &&
(TextureImage[46]=LoadBMP("Data/1/SpecWep.bmp")) &&
(TextureImage[47]=LoadBMP("Data/1/SpecWepmask.bmp")) &&
(TextureImage[48]=LoadBMP("Data/1/RescueChopper.bmp")) &&
(TextureImage[49]=LoadBMP("Data/1/RescueChoppermask.bmp")) &&
(TextureImage[50]=LoadBMP("Data/1/Light Flak.bmp")) &&
(TextureImage[51]=LoadBMP("Data/1/Medium Flak.bmp")) &&
(TextureImage[52]=LoadBMP("Data/1/Heavy Flak.bmp")) &&
(TextureImage[53]=LoadBMP("Data/1/Sam Bunker.bmp")) &&
(TextureImage[54]=LoadBMP("Data/1/Light Rubble.bmp")) &&
(TextureImage[55]=LoadBMP("Data/1/Flak Rubble.bmp")) &&
(TextureImage[56]=LoadBMP("Data/1/Flak Rubble.bmp")) &&
(TextureImage[57]=LoadBMP("Data/1/Flak Rubble.bmp")) &&
(TextureImage[58]=LoadBMP("Data/1/WeaponSelect.bmp")) &&
(TextureImage[59]=LoadBMP("Data/1/HUDStuff.bmp")) &&
(TextureImage[60]=LoadBMP("Data/1/Partisan.bmp")) &&
(TextureImage[61]=LoadBMP("Data/1/Partisanmask.bmp")) &&
(TextureImage[62]=LoadBMP("Data/1/Missile.bmp")) &&
(TextureImage[63]=LoadBMP("Data/1/Mig-25.bmp")) &&
(TextureImage[64]=LoadBMP("Data/1/Mig-23.bmp")) &&
(TextureImage[65]=LoadBMP("Data/1/Mig-29.bmp")) &&
(TextureImage[66]=LoadBMP("Data/1/Mig-X3.bmp")) &&
(TextureImage[67]=LoadBMP("Data/1/Mig-25Mask.bmp")) &&
(TextureImage[68]=LoadBMP("Data/1/Mig-23Mask.bmp")) &&
(TextureImage[69]=LoadBMP("Data/1/Mig-29Mask.bmp")) &&
(TextureImage[70]=LoadBMP("Data/1/Mig-X3Mask.bmp")) &&
(TextureImage[71]=LoadBMP("Data/1/Repair Truck.bmp")) &&
(TextureImage[72]=LoadBMP("Data/1/Repair Truck Mask.bmp")) &&
(TextureImage[73]=LoadBMP("Data/1/Cloud.bmp")) &&
(TextureImage[74]=LoadBMP("Data/1/Cloudmask.bmp")) &&
(TextureImage[75]=LoadBMP("Data/1/ControlTower.bmp")) &&
(TextureImage[76]=LoadBMP("Data/1/ControlTowerRubble.bmp")) &&
(TextureImage[77]=LoadBMP("Data/1/Pause.bmp")) &&
(TextureImage[78]=LoadBMP("Data/1/MissionComplete.bmp")) &&
(TextureImage[79]=LoadBMP("Data/1/Controls.bmp")) &&
(TextureImage[80]=LoadBMP("Data/1/Loading.bmp")) &&
(TextureImage[81]=LoadBMP("Data/1/MissionFailed.bmp")) &&
) // Second Image

glGenTextures(numtex, &texture[0]);

for (loop=0; loop<numtex; loop++)
glBindTexture(GL_TEXTURE_2D, texture[loop]);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);


for (loop=0; loop<numtex; loop++) // Loop Through All 5 Textures
if (TextureImage[loop]) // If Texture Exists
if (TextureImage[loop]->data) // If Texture Image Exists
free(TextureImage[loop]->data); // Free The Texture Image Memory
free(TextureImage[loop]); // Free The Image Structure
return Status; // Return The Status

Unfortunately, this is all pretty much modified NeHe code, so I probably forgot something in there that's sucking up memory.

Share this post

Link to post
Share on other sites
Kestrel    158
Actually the function is 'glDeleteTextures(GLsizei n,const GLuint *textures)'

Check out en-us/opengl/glfunc01_6vqr.asp

Share this post

Link to post
Share on other sites
_the_phantom_    11250
Part of your problem could well be the useage of the glAux library, it has memory leakes and you could well be getting caught by one of them. Switching to a better image loader is probably a good idea (see the Forum FAQ).

While NeHe is a good learning resource I really wouldnt use the code in a proper game.

Share this post

Link to post
Share on other sites
Kalidor    1087
As _the_phantom_ said, it's probably glAux leaking memory.

You can't tell how much memory your textures are taking up by looking at the memory usage of your program in the task manager. That tells you your system ram usage, but textures use video ram (most of the time) and there's no way to find out how much of that you are currently using. So when you're done with a texture, just call glDeleteTextures and the video driver will take care of freeing the video ram.

Share this post

Link to post
Share on other sites
Sir Sapo    769
Thanks guys, I'm going to replace the NeHe code with my own PNG loading function when I've got the game closer to completion, so I guess that should solve the problem.

Share this post

Link to post
Share on other sites
zedzeek    528
as kestrel said free(..) aint a opengl funcvtion its part of the language C
also u might wanna look at a better design than TextureImage[66]=LoadBMP("Data/1/Mig-X3.bmp")) &&
(TextureImage[67]=LoadBMP("Data/1/Mig-25Mask.bmp")) &&
eg perhaps store the filenames in a file, load the file read the names and then load them in a loop

Share this post

Link to post
Share on other sites
Kestrel    158
You know sometime ago a went real information about the glAux memory leak. If I remember correctly what I found was that the memory leak was not in the image loading functions but in the object creation functions like.
This is the only site that mentions were the memory leak/bugs are and doesn't say anything about the image loading functions.

Share this post

Link to post
Share on other sites
Sir Sapo    769
Yeah, I've looked into DevIL, and that looks like it is my best bet. I replaced my old function with one using DevIL and not I have no extra memory usage.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this