Boost logo

Boost :

From: E. Gladyshev (egladysh_at_[hidden])
Date: 2002-03-21 16:40:41


>If fool() doesn't want to know about ownership
>contracts why is it using
>shared_ptr<>?
>A smart_ptr is precisely about ownership contracts,
>and there is no way you
>can use it without caring about it.

fool() is using shared_ptr<> precisely because
it doesn't want to know anything about memory
allocation, he assumes that shared_ptr<> is
the smart guy.

Consider the following pseudo-code when there are
several
threads which share one object and that
shared_ptr<> is a thread safe implementation
of smart poniters.

int *g_p;

thread1()
{
    shared_ptr<int> p(new p);
    g_p = p.get();
    start_thread2();
    do something...
    make sure that the thread 2 is in the right state
    exit
}

thread2()
{
   fool( g_p );
}

fool( g_p )
{
   shared_ptr<int> p(g_p); //make sure that other
threads don't delete the object.
   ...
}

Another case when the same fool() crashes

int *g_p;
int p;

thread1()
{
    g_p = p;
    start_thread2();
    do something...
    make sure that the thread 2 is in the right state
    exit
}

thread2()
{
   fool( g_p );
}

fool( g_p )
{
   shared_ptr<int> p(g_p); //make sure that other
thread doesn't delete the object.
   ...
} ///!!!CRASH

I think it would be nice if shared_ptr<>
can work consistently even with pointer to
static objects. Why not!

Eugene

__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards®
http://movies.yahoo.com/


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