Like Tree1Likes
  • 1 Post By LyTning94

Is my SDL program leaking memory?

This is a discussion on Is my SDL program leaking memory? within the C++ Programming forums, part of the General Programming Boards category; I'm writing a program using SDL and have done quite a bit with it so far. As far as I ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    61

    Is my SDL program leaking memory?

    I'm writing a program using SDL and have done quite a bit with it so far. As far as I know I've used SDL_FreeSurface() on every SDL_Surface* I've used, have made sure to use TTF_CloseFont() on all the fonts I've opened with SDL_TTF, and have used SDL_Quit() and TTF_Quit() at the end of the program.

    Nevertheless, upon running runtime checker I get a ton of supposed leaks. Am I somehow not freeing memory?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Does your runtime checker give you any more information than that? In other words, what is the runtime checker looking for? It's possible that you are doing something silly, like allocating memory and then pointing the pointer to it somewhere else (you might see something like "unreachable memory" for that). Or it could be that the runtime checker is being overly paranoid. Without seeing what is actually there, vague generalities is the best I can do.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Unfortunately, I only have vague information as well, since I haven't purchased the full version of runtime checker and it doesn't show me very much info. If this helps any, these are some of the leaks I'm getting.

    malloc 6 occurance (5040 bytes)
    malloc 6 occurance (5554 bytes)
    malloc 3 occurance (3072 bytes)
    malloc 3 occurance (96 bytes)
    malloc (1024 bytes)
    malloc (8 bytes)
    except there's maybe like 75 of them.

    After running the program a few times, it appears to me that the more surfaces I blit, the larger the supposed leak is. My program is a card game and every time you play a card it obviously has to blit more surfaces. It seems that the more cards I play the more occurances I get. I don't know why blitting surfaces would be causing a leak though, since I free all the surfaces I blit at the end of the program.

    I have all the cards in the same image, so each time I blit I use a SDL_Rect object to blit only the section of the image I want. Could there be something wrong with blitting the same image (or different sections of it) time and time again before freeing?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    There is always the possibility that the leak is actually internal to the library, which means there isn't a lot you can do about it unless you also get the source code.

    The other thing to do is go through the relevant manual pages to check to see if there is any additional 'free' you should be doing.

    If you suspect say blitting a Rect, then perhaps create a small and "obviously no leaks" demonstration app that you can upload, which does the same thing over and over.

    > malloc 6 occurance (5040 bytes)
    Check for loops which run 6 times, or you create 6 instances of something.
    Are the sizes related to any image sizes, or any struct sizes?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    I free all the surfaces I blit at the end of the program
    Can you post some code maybe? It is a while since i used SDL but i dont recall only freeing surfaces at program end, its more something you do as you go along, except maybe the main screen surface, but is suppose if it is continually referring to the one sprite sheet then i suppose it makes sense to keep it constant. I thought there was a return value from SDL_FreeSurface() but checked docs and unfortunately not. Personally i would not use one sheet for the entire deck, unless the image is quite small.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Thanks for the replies. I figured out the problem on my own by commenting out certain functions and watching what runtime checker said. It turns out that I was repeatedly assigning the same image to a surface in a function which is called frequently, without freeing the surface before each time I reassigned it.
    Salem likes this.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    You may want to look into smart pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Leaking memory when creating threads
    By Elkvis in forum Windows Programming
    Replies: 3
    Last Post: 08-18-2009, 03:27 PM
  2. Am I leaking?
    By carrotcake1029 in forum C++ Programming
    Replies: 15
    Last Post: 07-16-2009, 05:09 AM
  3. STL leaking?
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2009, 02:47 AM
  4. Leaking Memory in a sendmail milter I wrote?
    By stevfletchcom in forum C Programming
    Replies: 8
    Last Post: 10-10-2008, 12:30 PM
  5. traping leaking memory
    By null in forum C Programming
    Replies: 5
    Last Post: 10-01-2001, 01:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21