Boost logo

Boost :

Subject: Re: [boost] scoped_ptr deleter
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2009-09-16 12:30:14


Berserker wrote:
> Stefan Strasser wrote:
>> the point of a deleter is to execute CUSTOM code at destruction.
>> using scoped_ptr (without deleter) automates deletion so you don't
>> have to write custom code that does that. that simplifies things a lot.
>> but a scoped_ptr delete would provide the same thing as a destructor
>> or a scope exit: execute custom code.
>> the only reason shared_ptr or unique_ptr do have deleters is because
>> the point of destruction aren't statically known, as they can be
>> copied or moved.
>
> My problem is simple: I have a custom memory manager (something similar
> to this one
> http://www.ogre3d.org/docs/api/html/OgreMemoryAllocatorConfig_8h.html )
> and when the scoped_ptr goes out of scope I need to invoke my custom
> deleter instead of the classic "delete".

Why not simply override operator delete?

The benefit of extending scoped_ptr is when you want to tweak memory
management for a subset of objects of the same type, which is quite
rare, IMO. It is so rare that it deserves its own pointer type. Most
often you'll just want to allocate all objects of the type the same way.
That's from my experience.

Another use case would be to use scoped_ptr as a scope guard releasing
some resource, such as FILE*. But I tend to think that traditional scope
guards and BOOST_SCOPE_EXIT are better tools for this.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk