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== 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== 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)
		printf("unable to initialize SDL: %s\n", SDL_GetError());
	SDL_Surface* mainWindow = SDL_SetVideoMode(DEFAULT_WINDOW_WIDTH,
	SDL_FillRect(mainWindow, NULL,
			SDL_MapRGB(mainWindow->format, 255, 255, 255));
	std::cout << "got here" << std::endl;
	return 0;

Open in new window

Who is Participating?
Wouter_Connect With a Mentor Commented:
Your code seems fine to me. I have seen more complaints about sdl having a leak. Most of the times it seems some underlying library is actually the culprit, not SDL. Maybe it is nothing to worry about. Check this for more information:

Yuo could also send an email to that mailinglist, the people there are very helpful and friendly.
evilrixSenior Software Engineer (Avast)Commented:
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."

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.

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.