Sign in to follow this  
templewulf

shared_ptr custom deallocator

Recommended Posts

I've googled (and icerocketed) custom deallocators to parameterize shared_ptr, but I haven't found how to create one. Really, I just want a no-op deallocator to make shared_ptr<SDL_Surface> not corrupt the heap when SDL_Quit and shared_ptr try to deallocate the same thing. Can anyone guide me to a better explanation of shared_ptr's custom deallocators? (Be gentle, I'm new to boost!)

Share this post


Link to post
Share on other sites
Here, check the docs for the second constructor, they describe what you are looking for I think.

You must pass a template parameter d (of type D) for which the expression d(p) is well formed (p is the raw pointer). For example:


void DeleteFn(SDL_Surface *p)
{
/* do what you need to p */
}

boost::shared_ptr< SDL_Surface > ptr(new SDL_Surface,DeleteFn);


should work; DeleteFn will be called when the pointer should be destroyed. I don't know SDL so my practice of "new SDL_Surface" is probably not the way your actually creating the surface, but you should get the idea.

Share this post


Link to post
Share on other sites
Oh...so I can just use a function pointer? I guess it was just TOO obvious, huh? [lol]

Is there a generic no-op deallocator in std or boost? Really, I just need it to not delete SDL_Surface. I could just use SDL_Surface*, but I'd like to know anyway.

I'd give you a Ratings++, but you're already 1337. You were awfully helpful! [smile]

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