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

Holger Brandsmeier brandsmeier at gmx.de
Mon Apr 23 22:52:26 CEST 2012


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_rcp.py
Type: application/octet-stream
Size: 437 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120423/25e74285/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: thisRCPPy.cpp
Type: text/x-c++src
Size: 1158 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120423/25e74285/attachment-0001.cpp>


More information about the Cplusplus-sig mailing list