Skip to main content
gamedev.net
🔒 Locked

Freeing Texture Memory

Started by Sir Sapo 10 replies 1.9k views
Original Post
Sir Sapo
Sir Sapo
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
My Current Project Angels 22 (4E5)
Sir Sapo
Sir Sapo
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(tileset==1) {	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")) &&      (TextureImage[82]=LoadBMP("Data/1/Smoke.bmp"))     )			// Second Image	{		Status=TRUE;									glGenTextures(numtex, &texture[0]);							for (loop=0; loop<numtex; loop++)							{			glBindTexture(GL_TEXTURE_2D, texture[loop]);			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );			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.
My Current Project Angels 22 (4E5)
Programmer16
Programmer16
I'll keep looking, but the only other solution I can find is to use glTexSubImage2D() (I don't even know if thats a solution, but I'll call it one.)
Kestrel
Kestrel
Actually the function is 'glDeleteTextures(GLsizei n,const GLuint *textures)'

Check out msdn.microsoft.com/library/ en-us/opengl/glfunc01_6vqr.asp
and http://www.google.com/search?hl=en&lr=&q=glDeleteTextures&btnG=Search
_the_phantom_
_the_phantom_
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.
Kalidor
Kalidor
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.
Sir Sapo
Sir Sapo
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.
My Current Project Angels 22 (4E5)
meatbeef
meatbeef
Maybe you should try using corona or DevIL instead?
zedzeek
zedzeek
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
Kestrel
Kestrel
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.
http://www.starstonesoftware.com/OpenGL/articles.htm
Sir Sapo
Sir Sapo
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.
My Current Project Angels 22 (4E5)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.