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

2012-04-25 Thread Dave Abrahams

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  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  wrote:
>>
>> on Mon Apr 23 2012, Holger Brandsmeier  wrote:
>>
>>> In fact the statement
>>>   class_, 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
>>> 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@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 
> #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 
> {
>   virtual int foo() = 0;
>
>   virtual ~K() {
> std::cout << "destructor for K called ..." << std::endl;
>   }
>
>   /*boost::shared_ptr getThisRCP() {
> return shared_from_this();
>   }*/
> };
>
> struct KWrapper : public K, public boost::python::wrapper, 
>   public boost::enable_shared_from_this 
> {
>
>   KWrapper() {
>
>   }
>
>   virtual int foo() {
> if( override f = this->get_override("foo") ) {
>   return f();
> } else {
>   return base_foo();
> }
>   }
>
>   boost::shared_ptr getThisRCP2() {
> return shared_from_this();
>   }
>
>   int base_foo() {
> return 0; 
>   }
> };
>
> BOOST_PYTHON_MODULE(thisRCPPy) 
> {
>   {
> typedef K ClassT;
> class_, boost::noncopyable >("K", 
> init<>() )
>   //.def("getThisRCP", &ClassT::getThisRCP)
>   .def("getThisRCP2", &KWrapper::getThisRCP2)
>   .def("foo", &ClassT::foo)
>   ;
>   }
> }
> ___
> Cplusplus-sig mailing list
> Cplusplus-sig@python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

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

2012-04-25 Thread Holger Brandsmeier
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_, 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
> 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  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  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  wrote:
>>>
>>> on Mon Apr 23 2012, Holger Brandsmeier  wrote:
>>>
 In fact the statement
   class_, 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
 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@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 
>> #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
>> {
>>   virtual int foo() = 0;
>>
>>   virtual ~K() {
>>     std::cout << "destructor for K called ..." << std::endl;
>>   }
>>
>>   /*boost::shared_ptr getThisRCP() {
>>     return shared_from_this();
>>   }*/
>> };
>>
>> struct KWrapper : public K, public boost::python::wrapper,
>>   public boost::enable_shared_from_this
>> {
>>
>>   KWrapper() {
>>
>>   }
>>
>>   virtual int foo() {
>>     if( override f = this->get_override("foo") ) {
>>       return f();
>>     } else {
>>       return base_foo();
>>     }
>>   }
>>
>>   boost::shared_ptr getThisRCP2() {
>>     return shared_from_this();
>>   }
>>
>>   int base_foo() {
>>     return 0;
>>   }
>> };
>>
>> BOOST_PYTHON_MODULE(thisRCPPy)
>> {
>>   {
>>     typedef K ClassT;
>>     class_, boost::noncopyable >("K", 
>> init<>() )
>>       //.def("getThisRCP", &ClassT::getThisRCP)
>>       .def("getThisRCP2", &KWrapper::getThisRCP2)
>>       .def("foo", &ClassT::foo)
>>       ;
>>   }
>> }
>> ___
>> Cplusplus-sig mailing list
>> Cplusplus-sig@python.org
>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
> --
> Dave Abrahams
> BoostPro Computing
> http://www.boostpro.com
>
> ___