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

Dave Abrahams dave at boostpro.com
Mon Apr 23 22:25:32 CEST 2012


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



More information about the Cplusplus-sig mailing list