SDL_Surface* DoSomething()
{
SDL_Surface* somesurface;
//... do something
return somesurface;
}
Local variable in SDL?
Hey I have a function like this:
Is there going to be a memory leak because I'm not using SDL_FreeSurface? or is the location being completely deleted when the function finishes?
Thanks for any clarification. :)
I assume inside the function you call SDL_LoadBMP, IMG_Load or SDL_CreateRGBSurface or something, and return the newly allocated surface. In this case the caller must clean it up.
If you are writing C++ you can simplify by writing a RAII wrapper over SDL_Surface instances:
Warning: code not compiled or tested.
SDL_Surface * DoSomething(){ return SDL_LoadBMP("foo.bmp");}void foo(){ SDL_Surface * surface = DoSomething(); SDL_BlitSurface(surface,...); // need to remember this line! SDL_FreeSurface(surface);}
If you are writing C++ you can simplify by writing a RAII wrapper over SDL_Surface instances:
#include <memory>#include <stdexcept>struct Surface{ Surface(const std::string &file) { surface = SDL_LoadBMP(file.c_str()); if(!surface) { throw std::runtime_error(SDL_GetError()); } } Surface(int w, int h) { surface = SDL_CreateRGBSurface(...); if(!surface) { throw std::runtime_error(SDL_GetError()); } } ~Surface() { SDL_FreeSurface(surface); } void blit(Surface &dest, const SDL_Rect &rect); void fillrect(const SDL_Rect &rect, Uint32 colour); // any other operationsprivate: SDL_Surface *surface; // disable copying Surface(const Surface &); Surface &operator=(const Surface &);};// or boost::shared_ptr, depending on intended usetypedef std::auto_ptr<Surface> SurfacePtr;SurfacePtr DoSomething(){ SurfacePtr somesurface(new Surface("foo.bmp")); //... do something return somesurface;}void foo(){ SurfacePtr surface = DoSomething(); surface->blit(...);} // SDL_FreeSurface called at end of scope
Warning: code not compiled or tested.
You need to understand how a pointer works. The code you have there allocates memory for a pointer to the SDL_Surface. IT does not, however, allocate the contents of an actual SDL_Surface.
If, for example, you use the function SDL_SetVideoMode, that function allocates the memory needed. Now, you will need to use SDL_FreeSurface.
If you need to understand pointers...
warning: may contain blue plasticine character with squeaky voice.
If, for example, you use the function SDL_SetVideoMode, that function allocates the memory needed. Now, you will need to use SDL_FreeSurface.
If you need to understand pointers...
warning: may contain blue plasticine character with squeaky voice.
Quote:Original post by speciesUnknown
If, for example, you use the function SDL_SetVideoMode, that function allocates the memory needed. Now, you will need to use SDL_FreeSurface.
SDL_SetVideoMode() returns a special SDL_Surface, which is explicitly documented as to not require a call to SDL_FreeSurface(). The video surface is destroyed when you call SDL_Quit() or SDL_QuitSubsystem(SDL_INIT_VIDEO), or when you call SDL_SetVideoMode() more than once.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement