View Full Version : Memory leaks

11-23-2009, 12:32 PM
I have a problem with my assignment ,im implementing a solar system screen saver ,at first i didnt use any textures , it worked fine smooth no problems,then when i used textures ,the memory usage continued to rise until it reaches 1.3GB then the program stops,i used free() delete to deallocate the textures and spheres but still it continues to rise. and here is part of the code,


//GLfloat* arr=&planet_emission[0];
glEnable ( GL_COLOR_MATERIAL ) ;
glMaterialfv( GL_FRONT, GL_EMISSION, planet_emission );
GLuint* boxTexture=(GLuint*)malloc(sizeof(GLuint));
FILE* file=(FILE*)malloc(sizeof(FILE));
case 2 :LoadTexture(file,boxTexture,"mercury.ppm", 256, 256, true);break;
case 3 :LoadTexture(file,boxTexture,"venus.ppm", 256, 256, true);break;
case 4 :LoadTexture(file,boxTexture,"Earth.ppm", 720, 360, true);break;
case 5 :LoadTexture(file,boxTexture,"mars.ppm", 256, 256, true);break;
case 7 :LoadTexture(file,boxTexture,"jupiter.ppm", 256, 256, true);break;
case 9 :LoadTexture(file,boxTexture,"saturn.ppm", 256, 256, true);break;
case 11:LoadTexture(file,boxTexture,"uranus.ppm", 256, 256, true);break;
case 13:LoadTexture(file,boxTexture,"neptune.ppm", 256, 256, true);break;
case 14:LoadTexture(file,boxTexture,"pluto.ppm", 256, 256, true);break;
GLUquadricObj* sphere = gluNewQuadric();
gluQuadricTexture(sphere, true);
gluQuadricNormals(sphere, GLU_SMOOTH);
glBindTexture(GL_TEXTURE_2D, *boxTexture);
gluSphere(sphere, Radius, 100, 100);
delete boxTexture;
delete file;

//any suggestions

11-23-2009, 12:38 PM
Don't load files or create resources (memory allocastions, new quadrics) in rendering loop. Do it only once - at program startup.

Also you are misusing delete and malloc. You can delete only what you have allocated with new. And everything you have malloc'ed you must free with free function. Or else you will get undefined behaviour at runtime.

11-23-2009, 12:41 PM
well this code is working ,i tried free and delete and both give the same output no difference

Alfonse Reinheart
11-23-2009, 01:03 PM
well this code is working ,i tried free and delete and both give the same output no difference

It doesn't matter that it is "working"; it's bad form and can lead to unpredictable errors. Don't do it.

Also, why are you allocating GLuints and FILE handles? Do you not know you can do this:

GLuint value;
GLuint *pValue = &value;

And FILE handles are pointers you get back from calling fopen and such; you never allocate them yourself.

That's probably where your memory leak is coming from: allocating FILE handles improperly.

11-23-2009, 01:11 PM
1. This is advanced OpenGL coding forum. Your question is related to beginner C++ and beginner OpenGL forums.
2. Load all your textures, before you draw first object. For each texture in file create one OpenGL texture object.
3. Check some OpenGL tutorials before you continue like http://nehe.gamedev.net. (http://nehe.gamedev.net) Above code works somehow, but it doesnt work properly. It is hard to change to work as you wish. It needs to be rewritten.

11-23-2009, 01:31 PM
thanks a lot worked ,it looks ridiculous but it works