# shared_ptr custom deallocator

## Recommended Posts

templewulf    526
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 on other sites
jpetrie    13161
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 on other sites
templewulf    526
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 on other sites
Fruny    1658
Quote:
 Is there a generic no-op deallocator in std or boost?

Not that I am aware of, but I trust you should be able to write one such. [grin]