• Advertisement
Sign in to follow this  

[SDL] Memory Leak! [Solved]

This topic is 4165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Help! I found a memory leak in an application I created. It seems to leak memory just from blitting the background to the screen. I have another program that does just that (displays an image to the screen) and it is leaking memory as well. Can anyone tell me what I have done wrong here?
#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_image.h"
#include <stdlib.h>

#define SCREEN_WIDTH	1280
#define SCREEN_HEIGHT	1024

SDL_Surface *screen, *background;
SDL_Rect rcBackground;

void loadGraphics()
{
	background = SDL_LoadBMP("../graphics/pingu.bmp");
	background = SDL_DisplayFormat(background);

	if(background == NULL)
	{
		printf("Failed to load background\n");
	}
}

void cleanUp()
{
	SDL_FreeSurface(background);
	SDL_Quit();
}

int main(int argc, char* argv[])
{
	SDL_Event event;
	bool quit = false;

	/* initialize video system */
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		printf("Unable to initalize SDL. Quitting\n");
		return -1;
	}

	/* create window */
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0,SDL_ANYFORMAT | SDL_DOUBLEBUF);

	loadGraphics();

	printf("The background was loaded bliting....\n");

	while (quit==false)
	{
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
			{
				case SDL_QUIT:
				 quit = true;
				 break;
			}
		}
		SDL_BlitSurface(background,NULL, screen,&rcBackground);
		SDL_Flip(screen);
		SDL_Delay(100);
	}
	cleanUp();
	return 0;
}


That's my whole app. Of course If I blit the image only once outside of the while loop I wouldn't lose any memory, but I'm trying to figure out why this one is loosing memory. Thanks! [Edited by - SigmaX on September 28, 2006 9:21:03 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by SigmaX
Help!

I found a memory leak in an application I created. It seems to leak memory just from blitting the background to the screen. I have another program that does just that (displays an image to the screen) and it is leaking memory as well. Can anyone tell me what I have done wrong here?

*** Source Snippet Removed ***

That's my whole app. Of course If I blit the image only once outside of the while loop I wouldn't lose any memory, but I'm trying to figure out why this one is loosing memory. Thanks!


Hmm... dont see anything wrong in your loop, but check your loadGraphics function.

SDL_DisplayFormat gives you a new surface, so you gotta free the other one. In your case you are assigning your surface to the new one, losing the other one and leaking.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

void loadGraphics()
{
SDL_Surface* temp = 0;
background = SDL_LoadBMP("../graphics/pingu.bmp");
temp = SDL_DisplayFormat(background);

if(!temp)
{
printf("Failed to load background\n");
}
else{background = temp;}

}


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
haha missed the free surface out and noticed somebody has posted the answer.

Share this post


Link to post
Share on other sites

Ok I modified the Load Graphics function to look like this

void loadGraphics()
{
temp = SDL_LoadBMP("../graphics/pingu.bmp");
background = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
if(background == NULL)
{
printf("Failed to load background\n");
}
}



It seems to still be leaking memory. Are my changes what you suggested or did I miss your point?

Thanks for your responces. :)

Share this post


Link to post
Share on other sites
Your code, as posted, does not leak during the loop. At least, it does not do so with the version of SDL I am using, which I got less than a month ago.

Share this post


Link to post
Share on other sites
Thanks again for your help guys. I found the memory leak and it seems it appears to be in some openGL libraries that we got from Intel. So yay! it wasn't me.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement