[C++-sig] weak_ptr to this in C++ classes extended from python

Holger Brandsmeier brandsmeier at gmx.de
Wed Apr 25 15:59:28 CEST 2012


Not all of it is commented out, it is enabled for the wrapper. You can
try to enable it for the class itself, but then I get a different
error, namely:

> In fact the statement
>  class_<IWrapper, boost::shared_ptr<IWrapper>, boost::noncopyable>("I", init<>() )
> has an issue. When I call getThisRCP() later, and the return value is
> not a null shared pointer, then I in fact the error
> TypeError: No to_python (by-value) converter found for C++ type: boost::shared_ptr<I>
> which is quite annoying. I also implemented Dave's suggestion with
> boost::enable_shared_from_this and because of the above issue I used
> this for the Wrapper and not the class itself (see K and KWrapper in
> the attachment).

-Holger

On Wed, Apr 25, 2012 at 15:32, Dave Abrahams <dave at boostpro.com> wrote:
>
> I don't understand; all the enable_shared_from_this stuff is commented
> out.  Can you please post the complete, correct example that you tried?
>
> on Mon Apr 23 2012, Holger Brandsmeier <brandsmeier-AT-gmx.de> wrote:
>
>> Dave,
>>
>> Here is the reduced example with only the
>> `boost::enable_shared_from_this` variant. The issues are all as
>> reported before.
>>
>> -Holger
>>
>> On Mon, Apr 23, 2012 at 22:25, Dave Abrahams <dave at boostpro.com> wrote:
>>>
>>> on Mon Apr 23 2012, Holger Brandsmeier <brandsmeier-AT-gmx.de> wrote:
>>>
>>>> In fact the statement
>>>>   class_<IWrapper, boost::shared_ptr<IWrapper>, boost::noncopyable
>>>>>("I", init<>() )
>>>> has an issue. When I call getThisRCP() later, and the return value is
>>>> not a null shared pointer, then I in fact the error
>>>> TypeError: No to_python (by-value) converter found for C++ type:
>>>> boost::shared_ptr<I>
>>>> which is quite annoying. I also implemented Dave's suggestion with
>>>> boost::enable_shared_from_this and because of the above issue I used
>>>> this for the Wrapper and not the class itself (see K and KWrapper in
>>>> the attachment).
>>>>
>>>> The behaviour of `enable_shared_from_this` is different from my
>>>> initual getThisRCP() / setThisRCP() solution but still has seriour
>>>> problems. Concider the code:
>>>>
>>>> class KDer(K):
>>>>   def __init__(self):
>>>>     K.__init__(self)
>>>>
>>>> k = KDer()
>>>>
>>>> In contrast to my initial solution the lines
>>>>   assert k.getThisRCP2() is not None
>>>>   k2 = k.getThisRCP2()
>>>> now work, which is nice.
>>>>
>>>> Also this call works now:
>>>>   print k2.foo();
>>>>
>>>> However, the following two lines
>>>>   del k
>>>>   print k2.foo();
>>>> give a segmentation fault. Note that the destructor for the class `K`
>>>> has not been called, which  would have printed a statement.
>>>
>>> Please post a reduced example without any of the "J" business that
>>> illustrates exactly the problem you're having for this one particular
>>> case.  If doing so doesn't immediately reveal the problem to you, I'm
>>> sure we can get to the bottom of it here.
>>>
>>>
>>> --
>>> Dave Abrahams
>>> BoostPro Computing
>>> http://www.boostpro.com
>>>
>>> _______________________________________________
>>> Cplusplus-sig mailing list
>>> Cplusplus-sig at python.org
>>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>
>> from thisRCPPy import *
>>
>> print '\ntestcase for thisRCP as from python extended class (using enable_shared_from_this):'
>> class KDer(K):
>>   def __init__(self):
>>     K.__init__(self)
>>
>> k = KDer()
>> assert k.getThisRCP2() is not None
>> k2 = k.getThisRCP2()
>> if 1:
>>   print k2.foo();
>>   del k
>>   print 'with the `del k` statement the following statement will crash the program:';
>>   print k2.foo();
>>   print 'this line is never reached';
>>   #del k
>>   del k2
>>
>>
>> #include <boost/python.hpp>
>> #include "iostream"
>> #include "string"
>> #include "boost/weak_ptr.hpp"
>> #include "boost/enable_shared_from_this.hpp"
>>
>> using namespace boost::python;
>> using namespace std;
>> //using Teuchos::RCP;
>>
>> struct K
>> // : public boost::enable_shared_from_this<K>
>> {
>>   virtual int foo() = 0;
>>
>>   virtual ~K() {
>>     std::cout << "destructor for K called ..." << std::endl;
>>   }
>>
>>   /*boost::shared_ptr<K> getThisRCP() {
>>     return shared_from_this();
>>   }*/
>> };
>>
>> struct KWrapper : public K, public boost::python::wrapper<K>,
>>   public boost::enable_shared_from_this<KWrapper>
>> {
>>
>>   KWrapper() {
>>
>>   }
>>
>>   virtual int foo() {
>>     if( override f = this->get_override("foo") ) {
>>       return f();
>>     } else {
>>       return base_foo();
>>     }
>>   }
>>
>>   boost::shared_ptr<KWrapper> getThisRCP2() {
>>     return shared_from_this();
>>   }
>>
>>   int base_foo() {
>>     return 0;
>>   }
>> };
>>
>> BOOST_PYTHON_MODULE(thisRCPPy)
>> {
>>   {
>>     typedef K ClassT;
>>     class_<KWrapper, boost::shared_ptr<KWrapper>, boost::noncopyable >("K", init<>() )
>>       //.def("getThisRCP", &ClassT::getThisRCP)
>>       .def("getThisRCP2", &KWrapper::getThisRCP2)
>>       .def("foo", &ClassT::foo)
>>       ;
>>   }
>> }
>> _______________________________________________
>> Cplusplus-sig mailing list
>> Cplusplus-sig at python.org
>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
> --
> Dave Abrahams
> BoostPro Computing
> http://www.boostpro.com
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig


More information about the Cplusplus-sig mailing list