SDL memory leak on initialization?

Posted on 2009-05-02
Last Modified: 2013-12-06
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

Question by:lungsponge121
    LVL 2

    Accepted Solution

    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.
    LVL 39

    Expert Comment

    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


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now