|
Boost : |
From: Juan Carlos Arevalo-Baeza (jcab.lists_at_[hidden])
Date: 2002-03-24 21:55:05
On Thu, 21 Mar 2002 11:33:09 +0100, Bjorn.Karlsson_at_[hidden] wrote:
>> void foo( int *x )
>> {
>> shared_ptr<int> xp( x );
>> }
>
>While I do think that shared_ptr is very smart, to be able to fix that code it would have to be ESP-equipped (not Extremely Smart Pointer - Extra Sensory Perception :-).
>
>When the shared_ptr goes out of scope, the reference count will become zero, and the stored pointer will be deleted (which in turn will do Very Bad Things to the stack). To force this code to work, a custom deleter that does nothing could be used (but why?).
This is why I devised a different manner of initialization on my (yes, I use my own, like many others) smart pointer library.
When a smart pointer is given a preallocated pointer, it assumes that someone already owns it, and wants to share this ownership, so the reference counter is initialized to 2 instead of 1. This gets rid of E's original problem. In order to cause the same bug with my implementation, the function above would need to be written as:
void foo( int *x )
{
shared_ptr<int> xp;
xp.Init( x );
}
or:
void foo( int *x )
{
shared_ptr<int> xp = shared_ptr<int>.Init( x );
}
or:
void foo( int *x )
{
shared_ptr<int> xp = InitSharedPtr( x );
}
Of course, there's still plenty of room for foot shots, but then, this is C++. You can always find a way to shoot your own foot using C++. :)
I'm sure that the designers of the BSP had reason to do it the way they did.
Salutaciones,
JCAB
email: jcab_at_[hidden]
ICQ: 10913692 @WORK: 101728263
WWW: http://www.JCABs-Rumblings.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk