[C++-sig] Bug and patch for boost.python withenable_shared_from_this
Nicolas Lelong
nico_ml at mgdesign.org
Fri Dec 12 10:48:35 CET 2008
Hi,
> Could you at least ask Peter Dimov if he thinks the
> dont_enable_shared_from_this patch could be generally useful?
> Maybe he's OK adding it, then it wouldn't be a big deal
> fixing Boost.Python, which would probably prevent a lot of
> confusion and lost time in the future.
I stumbled once again on this problem, so I took some time to bother Peter Dimov with the patch suggestion. Here, is a copy of our exchange :
Hello Peter,
I wanted to contact you about a problem that was raised, on february, on c++-sig mailing list about a problem happening when wrapping classes derived from 'enable_shared_from_this' using boost.python library.
The problem is briefly described in this post - including a patch to boost/shared_ptr.hpp that would allow to fix the problem.
http://mail.python.org/pipermail/cplusplus-sig/2008-February/012973.html
The original poster was encouraged by Ralf W. Grosse-Kunstleve to contact you about this, but we never heard from him on the list (http://mail.python.org/pipermail/cplusplus-sig/2008-February/013003.html).
To make it short, boost.python creates shared_ptr objects, holding the wrapped c++ objects, with a custom deleter managing the python object reference count. This leads to something like that :
#include <boost/enable_shared_from_this.hpp>
namespace {
class A : public boost::enable_shared_from_this<A>
{
public:
~A() {};
};
void my_deleter(void*)
{
}
};
BOOST_AUTO_TEST_CASE( test_enable_shared_from_this )
{
boost::shared_ptr<A> a( new A );
{
boost::shared_ptr<A> ater = boost::shared_ptr<A>( a.get(), my_deleter );
// OP patch proposal :: boost::shared_ptr<A> ater = boost::shared_ptr<A>( a.get(), my_deleter
// , boost::dont_enable_shared_from_this() );
BOOST_CHECK( a == ater );
}
boost::shared_ptr<A> abug = a->shared_from_this(); // this throws bad_weak_ptr
}
Did you hear about this problem ? Do you think this patch is viable and could be applied in boost trunk ? Do you happen to have another idea of workaround that could be applied in client code and/or in shared_ptr ?
Thanks for your time,
best regards,
Nicolas Lelong
mgdesign
Followed by Peter's answer that lead to Trac ticket #2584 :
Yes, I am aware of this problem. It is possible today to emulate the
proposed functionality by using the aliasing constructor, but I think that a
more proper fix for the issue is to make enable_shared_from_this more
intelligent so that it doesn't reinitialize itself in such scenarios.
I'll appreciate it if you file a Trac ticket for this and assign it to me.
I've a few pending enable_shared_from_this issues anyway, and I'll try to
find the time to address them before the next release ships.
--
Peter Dimov
http://www.pdplayer.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081212/4c26663e/attachment-0001.htm>
More information about the Cplusplus-sig
mailing list