When To Free Surfaces

Started by
3 comments, last by rip-off 15 years ago
I'm currently working on making a tetris clone in C++ w/ SDL, and things are starting to come together. I was playing what I have done so far looking for bugs or ways to improve when I realized that my RAM usage monitor was reading 85% (of 4 gb.) I checked the game was using ~1600 mb of ram after about 10m of play O_o I obviously have some serious memory leak, and I'm questioning how to free surfaces now. Should I be freeing every surface every frame? If so, does this mean I need to load each image file each frame? How do I know when it's time to free a surface? When I do free a surface, what's the best way of getting it again for the next frame? I'm very confused, any help would be greatly appreciated :D
Advertisement
Well lets see i guess one way to do it would be to Load your textures and or images in and keep them as master files and when a new tetris piece exists give it a refrence to the master texture or image.

The above method means that you will never have more memorie used then the master texture list.

Another way to do it is.
if you Tetris piece is a class witch i hope it is you can use the Deconstructor to free the surface the piece was using.

Look up the Deconstructor ether via Google or MSDN.

I hope that helps you out.

EDIT: One thing that is important to note is that When using pointers to the master texture list is when you are done to clean up by making the pointer equal null this way your program wont behave unexpectedly if it access that pointer after it should.



Regards Jouei.
Quote:Original post by Maevik
I'm currently working on making a tetris clone in C++ w/ SDL, and things are starting to come together. I was playing what I have done so far looking for bugs or ways to improve when I realized that my RAM usage monitor was reading 85% (of 4 gb.) I checked the game was using ~1600 mb of ram after about 10m of play O_o


You should be awake that the Task Manager is an amazingly poor way to measure how much memory your program is actually using.

Quote:I obviously have some serious memory leak, and I'm questioning how to free surfaces now.


You free them by calling SDL_FreeSurface(). :D

Have you considered the possibility that you do something *else* that leaks memory, other than loading surfaces and not freeing them?

Quote:Should I be freeing every surface every frame? If so, does this mean I need to load each image file each frame?


No and no.

Quote:How do I know when it's time to free a surface?


When you're done with it.

Quote:When I do free a surface, what's the best way of getting it again for the next frame?


By loading it again. But since you don't free the surface until you don't expect to need it again (at least not for a while), this isn't an issue.

The only tricky part is making sure that you do actually free every surface that you load. There are ways to organize your code that make it easier to get this working properly. You might want to consider using a smart pointer class, such as boost::shared_ptr. You can set that one up to use SDL_FreeSurface() for cleanup (by default it would try to call 'delete' on the pointer, which would be a very bad thing to do with an SDL_Surface*).
I wasn't using task manager to find memory usage, I have a widget for my WinSB, although, it could be just as inaccurate for all I know. It's supposed to be pretty good though.

I wasn't loading each surface each frame, but I was loading a surface for each object, so that's where I need to do some revision.

In any case, thanks guys. This was helpful,
If you are using SDL_TTF, remember to free the old pointers when you are done with them. The same applies if you are using SDL_gfx to rotate or scale surfaces. For such a huge memory leak, I imagine whatever you are allocating you are doing almost every frame.

This topic is closed to new replies.

Advertisement