[C++-sig] Retaining a useful weak_ptr to a wrapped C++ interface implemented in Python

Adam Preble adam.preble at gmail.com
Tue Mar 6 03:23:26 CET 2012


I am trying to break a smart pointer cycle in my application.  There is an
interface that can be implemented in both c++ and Python, and it is used
for messaging callbacks.  I can contain a list of these as shared pointers,
but it only makes sense for cases where the owner of the container has
exclusive control of the object in question.  These work fine in both c++
and Python.  I decided for the messaging callback list I would switch them
to weak_ptr's.

I get into trouble if I register a Python implementation of the callback
with ownership staying within Python.  When it comes time to communicate a
message back, I see that the weak_ptr has:

use_count = 0
weak_count = 1

The pointer fails to cast.  Meanwhile, the object is looks very much alive
in Python.  I have a destructor in the c++ definition interface for
debugging and I see it never gets called.  Similarly, __del__ doesn't get
called on the Python implementation, although I am lead to believe I can't
trust it.  Perhaps most compelling is that I can continue to poke and prod
at the object in the Python shell well after this failed weak_ptr cast.

I am wondering if there is anything I can do for the cast to succeed.  At
this point all I can think of is making the owner of the container get a
specific reference to the blasted thing, which I'd rather not have to do.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120305/2f8ef878/attachment.html>


More information about the Cplusplus-sig mailing list