From: E. Gladyshev (egladysh_at_[hidden])
Date: 2002-03-21 12:41:20
I like this name Extra Sensory Perception, LOL.
This sample could be a real code. The problem
is with foo() of shall we call it fool().
The fool() function could be a generic function
that is called from different contexts.
fool() is very dumn and doesn't want to learn
anything about memory allocation tricks.
Fortunately fool() knows a very smart guy, named
shared_ptr<>,... but is he really smart?
=== From Bjorn ====
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
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?).
If the example correlates to some real code, the use
of shared_ptr is unnecessary in the first place,
because the int is allocated on the stack, and can't
possibly go out of scope whilst in function foo.
> -----Original Message-----
> From: E. Gladyshev [mailto:egladysh_at_[hidden]]
> Sent: den 21 mars 2002 06:25
> To: boost_at_[hidden]
> Subject: [boost] shared_prt, will it crash?
> A smart pointer question,
> will this code crash?
> void foo( int* p );
> int x;
> foo( &x );
> return 0;
> void foo( int *x )
> //foo() has no idea how 'x' has been allocated
> //but shared_ptr<> is smarter then foo() is.
> //It is a smart pointer after all.
> shared_ptr<int> xp( x );
> //... call some other functions with x or xp ...
> //shared_ptr<> should make sure that the
> //object is not deleted while foo() is using it.
> *xp = 0;
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards®
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk