Sign in to follow this  
Serenity54

Local variable in SDL?

Recommended Posts

Hey I have a function like this:
SDL_Surface* DoSomething()
{
    SDL_Surface* somesurface;
    
    //... do something
    
    return somesurface;
}


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. :)

Share this post


Link to post
Share on other sites
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.


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 operations
private:
SDL_Surface *surface;

// disable copying
Surface(const Surface &);
Surface &operator=(const Surface &);
};

// or boost::shared_ptr, depending on intended use
typedef 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.

Share this post


Link to post
Share on other sites
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...
http://www.youtube.com/watch?v=6pmWojisM_E

warning: may contain blue plasticine character with squeaky voice.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this