Memory Leak - A few question about SDL

I am programming a small platformer game, and I guess I have a huge memory leak. The game’s memory usage keeps growing and growing.
I have currently no plans on showing the code, since you wouldn’t be able to read the mess. But if it is needed, be my guest.

So, is there something I should be aware of, using the SDL-libraries? (SDL and SDL_image)
And when are we supposed to draw stuff on the screen? What I am currently doing, is drawing everything on the screen every time the code runs. It’s a part of the game loop.
While drawing stuff on the screen, I get the sprite’s position in a text-file (which is made by my Map Editor).

Nechs wrote:

I am programming a small platformer game, and I guess I have a huge memory leak. The game’s memory usage keeps growing and growing.
I have currently no plans on showing the code, since you wouldn’t be able to read the mess. But if it is needed, be my guest.

So, is there something I should be aware of, using the SDL-libraries? (SDL and SDL_image)
And when are we supposed to draw stuff on the screen? What I am currently doing, is drawing everything on the screen every time the code runs. It’s a part of the game loop.
While drawing stuff on the screen, I get the sprite’s position in a text-file (which is made by my Map Editor).

Without the code there isn’t really much we can say but

  1. I am guessing that by “every time code runs” you mean “every frame” / “every iteration” and that is popular approach.

  2. You are probably making new sprite / SDL_Surface / SDL_Texture (which SDL version are we talking about?) every time you want to put it on screen. You should create it only once and then draw it multiple times. Same with getting sprite position from file before drawing, read them once and store for later.

  3. http://www.google.pl/search?q=sdl+tutorials

Sounds like a challenge. Your program must be a real piece of work,
considering things that are even supposed to be readable at all can
still get hacked.

If you’re wondering if its the way you’re using SDL or if there’s a
big time loose-end somewhere else in your code…

You could always reprogram the rendering part, to use ncurses instead.

Best part, it’ll work ever telnet too.On Wed, Sep 1, 2010 at 12:56 PM, Nechs wrote:

I have currently no plans on showing the code, since you wouldn’t be able to
read the mess.

?? 2. Memory Leak - A few question about SDL. (Nechs)

Wednesday,
September 1, 2010 9:56 AM

        From: 
        "Nechs"
    	To: 
    	sdl at lists.libsdl.org

I am programming a small platformer
game, and I guess I have a huge memory leak. The game’s memory usage
keeps growing and growing.

I have currently no plans on showing the code, since you wouldn’t be
able to read the mess. But if it is needed, be my guest.

So, is there something I should be aware of, using the SDL-libraries?
(SDL and SDL_image)

And when are we supposed to draw stuff on the screen? What I am
currently doing, is drawing everything on the screen every time the code
runs. It’s a part of the game loop.

While drawing stuff on the screen, I get the sprite’s position in a
text-file (which is made by my Map Editor).

Tactics to try to figure out what is causing the leaks…

1.? Use a tool to find them.

  • If you are on Windows and using a Microsoft compiler, maybe look at VLD (Visual Leak Detector).? If you are Linux and Mac, someone else will have to speak up as I’m not really familiar enough with them.

  • wrap all your allocs/creates/news and frees/destroys/deletes in your own leak tracker and report what has never been cleaned up at the end of the program.

  1. Stub out code to see how it affects memory growth.? This means you are crippling your program but the goal is to find out or narrow down where the leaks are.? This is also a tactic when finding out of bounds memory writing/corruption.? Once you find the chunk(s) in question look to see how you are handling the clean up so that it matches.— On Wed, 9/1/10, sdl-request at lists.libsdl.org wrote:

From: zekaric@yahoo.ca (Robbert de Groot)
To: sdl at lists.libsdl.org
Sent: Wed, September 1, 2010 4:40:18 PM
Subject: [SDL] Memory Leak - A few question about SDL.

If you are Linux and Mac, someone else will have to speak up as I’m not really
familiar enough with them.

valgrind is the tool for use in linux (wikipedia also tells me it works on
macos)

Reading the original problem, I would agree (with out seeing source) is that
SDL_surface’s are being creating every loop without being released.

Thanks to you all! I will try my best to follow your advice.

What I will do next, is to separate the code into different header-files and make the code a lot easier to read. And delete unused variables.
If the problem still prevail, I can post the code without making your eyes bleed of the horror.

Nechs wrote:

Somehow, “surTextScore = TTF_RenderText_Solid( font, text, textColor );” and two more of those, was stealing my memory.

Well… did you call SDL_FreeSurface(surTextScore) to free the surface that TTF_RenderText_Solid() is creating?

jlunderville wrote:

Nechs wrote:

Somehow, “surTextScore = TTF_RenderText_Solid( font, text, textColor );” and two more of those, was stealing my memory.

Well… did you call SDL_FreeSurface(surTextScore) to free the surface that TTF_RenderText_Solid() is creating?

Yes, I did - at the end of the program (outside of the main loop).------------------------
Important information: Using the newest SDL-library, Windows Vista as my OS, Dev-C++ as my IDE (and VC++)

jlunderville wrote:

Nechs wrote:

Somehow, "surTextScore = TTF_RenderText_Solid( font, text, textColor );"
and two more of those, was stealing my memory.

Well… did you call SDL_FreeSurface(surTextScore) to free the surface that
TTF_RenderText_Solid() is creating?

Yes, I did - at the end of the program (outside of the main loop).

Well, there’s the problem. You need to free it soon after you’ve blitted
that surface (in that same iteration of your main loop).
TTF_RenderText_Solid, like many functions, creates a new SDL_Surface every
time; it doesn’t reuse the same one.On Thu, Sep 2, 2010 at 9:55 AM, Nechs wrote:

You need to free it soon after you’ve blitted that surface (in that same
iteration of your main loop).

Not necessarily. You need one call to SDL_FreeSurface() for every time you
TTF_RenderText_*(). But you can save a lot of time by only re-creating the
text surface when the text actually changes. This is a fairly easy
optimisation.

A common pattern is something like this psuedo-code:

string text;
SDL_Surface *text = TTF_RenderText(“Initial String”);

while(running)
{
if(textUpdated())
{
SDL_Surface temp = TTF_RenderText(text);
if(temp)
{
SDL_FreeSurface(text);
text = temp;
}
else { /
handle error */ }
}

 SDL_BlitSurface(text, screen);
 SDL_Flip(screen);

}

SDL_FreeSurface(text);

For what it’s worth, I know when I started getting into some real
programming with C and C++, I was thinking too much like I was working in
Java. I used way too many dynamic allocations to keep track of. Use scopes
to manage your memory if you can.

Jonny DOn Wed, Sep 1, 2010 at 6:46 PM, Nechs wrote:

Thanks to you all! I will try my best to follow your advice.

What I will do next, is to separate the code into different header-files
and make the code a lot easier to read. And delete unused variables.
If the problem still prevail, I can post the code without making your eyes
bleed of the horror.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org