Thank you for your replay.

You mean:
when Python call C++ function , it will pass a RAW pointer (NOT a shared_ptr) to C++, and when pointer implict convert to a shared_ptr<T>, we got a totally different shared_ptr to the pointer, so that the _internal_weak_this will be reinitialized .

Is that right?

2008/8/4 Peter Dimov <pdimov@pdimov.com>
I can at least explain why the problem occurs, but I'm not sure if I can suggest a solution at this time.

The problem is that inside X::test there are two separate shared_ptr instances pointing to the same A object. One has been created in main(), and another has been created by Boost.Python for the x.test( a ) call in t1.py.

It so happens that the current behavior of enable_shared_from_this is for the second instance to override the first one; that is, _internal_weak_this is reinitialized from the 'pObj' argument when it's created.

Once 'pObj' goes out of scope, the weak_ptr expires, and shared_from_this starts throwing bad_weak_ptr.

By using a raw pointer argument, you avoid this reinitialization.

We've been thinking of changing the behavior of enable_shared_from_this in such cases, making it ignore subsequent shared_ptr instances to the same object and having it stick to the first one. This, however, will not happen for the upcoming 1.36 release of Boost.

You can in principle achieve the same result by patching your local copy of Boost to make weak_ptr::_internal_assign only initialize when this->expired() is true, but this has never been tested.

_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users