"Connecting" SDL_Surface to shared_ptr

Go To StackoverFlow.com


I'd like to know how can I connect SDL_Surface* with shared_ptr?
I need to call SDL_FreeSurface(SDL_Surface*) before I delete SDL_Surface. How can I "modify deletion process" in shared_ptr?

2012-04-03 21:27
by fex


Just pass SDL_FreeSurface to the constructor:

std::shared_ptr<SDL_Surface> shared_surf(SDL_LoadBMP("foo.bmp"), SDL_FreeSurface);

Just be sure you don't do this with the pointer returned by SDL_SetVideoMode or SDL_GetVideoSurface.

2012-04-03 21:47
by Benjamin Lindley
Why shouldn't you do this with these? (SDL_SetVideoMode etc.. - user1511417 2013-05-21 11:35
@user1511417: As per the documentation. "The surface returned is freed by SDL_Quit() and should nt be freed by the caller."Benjamin Lindley 2013-05-21 20:43