Memory management, mainly using SDL

Started by
16 comments, last by Tallkotten 11 years, 5 months ago
Hello again
You are sure this snippet is leaking.

SDL_Surface* tempSurface = imageLoader.loadImg("example_image.png");
SDL_BlirSurface(tempSurface, NULL, screen, NULL);


Does that mean loadImg creates a new surface pointer everytime you call imageLoader.loadImg("example_image.png"); ? If so, then you really have to free that surface via imageLoader.unload as soon as you don't need it (i.e. in the update(int) function), because as soon as you exit that update function, you lose access to that surface. If however I guessed wrong, and your loader does cache the surface pointers, then it's all cool. However you did observe steadly growing memory usage, so I'm pretty sure you are creating a new copy with every call to the loader.
Advertisement

Hi, if you are using C++, perhaps you can use a smart pointer for this (These two lines looks ugly as sin, but provides memory safety I have yet to see with any other language / platform)...


std::tr1::shared_ptr<SDL_Surface> surface;
surface.reset(SDL_LoadImage(imageFileName.c_str()), std::tr1::bind(SDL_FreeSurface, std::tr1::placeholders::_1));
// Now you can do whatever and RAII will take care of the memory management.


Remember: You must set the deleter to be SDL_FreeSurface, because otherwise (and in std::auto_ptr) the c++ delete will just be called, which will cause undefined behaviour with things not created via new.

I generally do this with OpenGL textures but there is no reason this wont work for SDL textures.


Yes that is one option i might go with later on when i add the manager system. Like i described earlier i dont really have to unload images at the moment since i have so few loaded.

But i'll definitely consider it!


If you are not using the very latest compiler you will need to include the technical report 1 versions of memory and functional.
#include <tr1/functional>
#include <tr1/memory>


How do i know if i have to do that or not? And what difference does it make?

I am running codeblocks
I just found out where one of the leaks were located... In one of my functions where i turn a string to a char* array i never used delete after new...

Thanks everyone! Hopefully i wont get stuck on the rest of the code (that i have yet to check for leaks :)).

How do i know if i have to do that or not? And what difference does it make?


If you get a compiler error stating that std::tr1::shared_ptr does not exist, then you will need to include the tr1 versions of the header.

If you are using the CodeBlocks IDE, that probably means you are using GCC/G++ as your compiler, so it is quite likely you will need to include them.
http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.
So why are you using `new' and `delete' for things like a "char * array"? You probably should be using std::vector or std::string instead of using new to allocate arrays. It's much harder to have a memory leak if you don't use `new'.

[quote name='Tallkotten' timestamp='1352366168' post='4998799']
How do i know if i have to do that or not? And what difference does it make?


If you get a compiler error stating that std::tr1::shared_ptr does not exist, then you will need to include the tr1 versions of the header.

If you are using the CodeBlocks IDE, that probably means you are using GCC/G++ as your compiler, so it is quite likely you will need to include them.
[/quote]

Ok thanks! Yes i am using GCC/G++ :)


So why are you using `new' and `delete' for things like a "char * array"? You probably should be using std::vector or std::string instead of using new to allocate arrays. It's much harder to have a memory leak if you don't use `new'.


Well that is old code and when i looked up how to do it that was one of the only ways. Also TTF_RenderText_Solid requires a char*. http://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf_43.html
No, it requires a const char *, which you can from a std::string with the .c_str() member function.

No, it requires a const char *, which you can from a std::string with the .c_str() member function.


Well... f*ck me...

I've used that to some extent but now i just feel silly... Is it even worth going back and using that instead of my newly implemented system?

This topic is closed to new replies.

Advertisement