[SDL] Mouse Input + Delay problem

CWC charlesw at blackfoot.net
Mon Apr 7 10:58:31 PDT 2008


Ankush Thakur wrote:
> I'm attaching my main.cpp source file with this mail.
> 
> The trouble is that I have to handle a game variable called gameRunning.
> 
> So, the structure now is...
> 
> main()
>    while gameRunning
>       while frame time not elasped...
>          draw
>          check for clicks
> 
>     ........... etc etc.
> 
> The problem is that the game in eating increasing amounts of RAM per 
> second. I guess the reason is the two while loops or maybe I'm wrong.
> 
The reason you're leaking memory is that you're creating a new SDL_Rect 
everytime you use one. You only need one ... ie
SDL_Rect temp_Rect;
// set rect params
// pass the address like so
engine.draw(tmp_Surface, &tmp_Rect);

// change parameters and use it again.
engine.draw(tmp_Surface, &tmp_Rect);

This of course assumes that your engine is just using the values in each 
call and not storing the address for some future use.

There are other places where you're "newing" data, but there are no 
deletes. When you're allocating memory in a loop and you never delete 
it, you'll be out of memory soon.

CWC


More information about the SDL mailing list