Link to home
Start Free TrialLog in
Avatar of lungsponge121
lungsponge121

asked on

SDL memory leak on initialization?

I started writing things in SDL - I have never used SDL before about a week ago and have been self-teaching through online tutorials and the SDL API.

I have valgrind setup to check for leaks everytime I build - the only problem is, without any of my code inside the SDL i get a leak of 2150 bytes.  Does anybody know what could be causing this.

The valgrind report is as follows:

got here
==3973==
==3973== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 342 from 5)
==3973== malloc/free: in use at exit: 114,664 bytes in 2,367 blocks.
==3973== malloc/free: 16,478 allocs, 14,111 frees, 2,346,209 bytes allocated.
==3973== For counts of detected errors, rerun with: -v
==3973== searching for pointers to 2,367 not-freed blocks.
==3973== checked 598,768 bytes.
==3973==
==3973== LEAK SUMMARY:
==3973==    definitely lost: 2,370 bytes in 16 blocks.
==3973==      possibly lost: 10,784 bytes in 300 blocks.
==3973==    still reachable: 101,510 bytes in 2,051 blocks.
==3973==         suppressed: 0 bytes in 0 blocks.


int main(int argc, char **argv)
{
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0)
	{
		printf("unable to initialize SDL: %s\n", SDL_GetError());
		exit(-1);
	}
	int optBPP = SDL_VideoModeOK(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT,
			DEFAULT_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF);
	SDL_Surface* mainWindow = SDL_SetVideoMode(DEFAULT_WINDOW_WIDTH,
			DEFAULT_WINDOW_HEIGHT, optBPP, SDL_HWSURFACE | SDL_DOUBLEBUF);
	SDL_WM_SetCaption(DEFAULT_WINDOW_TITLE, 0);
	SDL_FillRect(mainWindow, NULL,
			SDL_MapRGB(mainWindow->format, 255, 255, 255));
	SDL_Flip(mainWindow);
 
	SDL_FreeSurface(mainWindow);
	SDL_Quit();
	std::cout << "got here" << std::endl;
	return 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Wouter_
Wouter_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of evilrix
Just an observation, which may or may not make a difference but the reference for SDL_SetVideoMode states, "On success. The returned surface is freed by SDL_Quit and must not be freed by the caller. This rule also includes consecutive calls to SDL_!SetVideoMode (i.e. resize or resolution change) because the existing surface will be released automatically. Whatever flags SDL_!SetVideoMode could satisfy are set in the flags member of the returned surface."

http://www.libsdl.org/cgi/docwiki.cgi/SDL_SetVideoMode

Valgrind's detailed [full|yes] leak detector report should give you specific info about where the leaked blocks started life.

-leak-check=<no|summary|yes|full> [default: summary]
When enabled, search for memory leaks when the client program finishes. A memory leak means a malloc'd block, which has not yet been free'd, but to which no pointer can be found. Such a block can never be free'd by the program, since no pointer to it exists. If set to summary, it says how many leaks occurred. If set to full or yes, it gives details of each individual leak.

http://valgrind.org/docs/manual/mc-manual.html#mc-manual.flags

It might be a "static leak", where heap is allocated once to a global and is then reused but never freed. Example below. Try putting this code into a function and calling it a couple of times. If the leak doesn't get worse it's almost certainly a static leak and one you can probably live with (since it's not going to get worse).

Other than that try commenting out a bit at a time until the leak goes away. The last thing you commented out is almost certainly going to be the cause.

// This demonstrates an example of a static leak.
 
char * staticBuffer;
 
int main(int argc, char **argv)
{
	 staticBuffer = new char[100];
}

Open in new window