Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualKarsten_

Posted 07 November 2012 - 01:15 PM

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.

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>

#3Karsten_

Posted 07 November 2012 - 01:14 PM

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 almost everything.

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>

#2Karsten_

Posted 07 November 2012 - 01:12 PM

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) delete will just be called, which will cause undefined behaviour.

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

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>

#1Karsten_

Posted 07 November 2012 - 01:10 PM

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.

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

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>

PARTNERS