|
Boost Users : |
From: Noah Roberts (roberts.noah_at_[hidden])
Date: 2008-05-16 12:56:33
Sohail Somani wrote:
> I was recently torn between doing this or thinking whether I'm doing it
> wrong. I'm still not sure whether using shared pointers everywhere with
> null deleters for stack allocated objects is The Right Thing. It kind of
> defeats the purpose of shared_ptr which is to ensure that the lifetime
> of the pointer is guaranteed for you. With null deleters, maintenance
> programmers may do the wrong thing (stuff something in an event queue
> deep down, for example.)
>
> If anyone has any opinion (for or against) please shout!
>
I certainly would not use them everywhere. That's just a waste as was
expressed by another.
The only time I think one would be justified in using a shared_ptr with
a null deleter for a stack object is when passing one into a function
that uses it. However, even this is dangerous since the fact that this
function is using a shared_ptr indicates that it could make a copy of
it! So you better be pretty darn sure of the lifetime of that
shared_ptr so that it doesn't outlive your stack and get called
somewhere down the line...pointing at stack area memory!
You could return one to some internal data but this also seems like
highly questionable design to me. All copies of that shared_ptr that
you just handed out, pointing to your insides, better not outlive
you...and you have 0 control over that.
Putting an object into a shared_ptr is pretty much saying, "I don't care
when this object is deleted but it better stick around long enough to be
used by everyone that wants to do so." Stack objects don't fit this
sentence at all.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net