Memory leak

I posted recently a topic where I had some questions about light but now after that being resolved there is another thing.After pressing a certain key Moon rotates around Earth (my scene is about that)but in a few moments the memory starts to leak.My first guess is that I’m doing something wrong with textures so here is that part of the code:


void Texture()
{
	glGenTextures(2,tekstura);

	if(!glIsTexture(tekstura[0]))
	{
		glBindTexture(GL_TEXTURE_2D,tekstura[0]);
		//AUX_RGBImageRec *teximage;
		memset(teximage,0,sizeof(void *)*1);

		teximage[0] = auxDIBImageLoad("Earth1.bmp");
		glPixelStoref(GL_UNPACK_ALIGNMENT,1);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, teximage[0]->sizeX,
			teximage[0]->sizeY,0, GL_RGB,GL_UNSIGNED_BYTE,teximage[0]->data);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );

		free(teximage[0]->data);
		free(teximage[0]);
		
	}

	 if(!glIsTexture(tekstura[1]))
	{
		glBindTexture(GL_TEXTURE_2D,tekstura[1]);
		//AUX_RGBImageRec *teximage;
		memset(teximage,0,sizeof(void *)*1);

		teximage[1] = auxDIBImageLoad("MoonMap.bmp");
		glPixelStoref(GL_UNPACK_ALIGNMENT,1);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, teximage[1]->sizeX,
			teximage[1]->sizeY,0, GL_RGB,GL_UNSIGNED_BYTE,teximage[1]->data);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );

		free(teximage[1]->data);
		free(teximage[1]);
	
	}
}

Maybe there is something wrong there?

You don’t call this Texture() each frame right ?

Not anymore :slight_smile: I’ve placed the function call in Reshape method and there is no leak.Thanks!

You should call this function only once, when you have created the context and are loading the data. It’s place is not in the ‘reshape’ event but rather in the initialization step.

And even if you called it every frame, code should not leak memory. So by doing it less often, you only prevent it from doing as much damage, as before, but you didn’t fix it.

I don’t know how AUX_RGBImageRec works, but there are a few suspicious things:

memset(teximage,0,sizeof(void *)*1);

What black magic is this supposed to be ? This looks extremely weird to me.

free(teximage[1]->data);
free(teximage[1]);

Here you free two things, the “data” and the “image”. Though you didn’t allocate any of those yourself (through “malloc”). Either you need to use a completely different function from the aux-library, or i guess you only need to free the image, since that should take care of freeing the data (ONCE!) itself.

Jan.

Well, he would be creating two textures each frame and not deleting them, so the memory leak is apparent.

and that memset statement does basically nothing, it just shows fundamental misunderstanding of pointers and memory…