[C++-sig] boost python and weak_ptr from a shared_ptr argument

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Wed Nov 25 21:13:50 CET 2009


This is a known problem. Boost.Python builds shared_ptr instances when calling your functions, using a custom deleter. I think internally it just uses regular Python reference counting to manage the lifetime of your KBObject.

Unfortunately I don't have a nice suggestion for you. A few years ago I worked around the issue in this way:

struct KBOject_data {
  std::string name;
};


struct KBOject {
  boost::shared_ptr<KBOject_data> data;
};

I.e. by hiding the shared_ptr from Boost.Python.
It isn't pretty. Maybe someone else has a better solution?
(I've always been wondering if enable_shared_from_this could make this nice.)


----- Original Message ----
From: Jahn Fuchs <jfuchs at esigma-technology.com>
To: cplusplus-sig at python.org
Sent: Wed, November 25, 2009 8:16:58 AM
Subject: [C++-sig] boost python and weak_ptr from a shared_ptr argument

Hello boost python users,

I really like boost python but I stumbled in a problem I don't know how to solve, maybe you have an idea:

I have a class KnowledgeBase that holds an shared_ptr to an KBObject and also a weak_ptr to an KBObject.  If I set the shared_ptr with the set_shared_ptr function it works fine, but it doesn't work with the set_weak_ptr function (which takes a shared_ptr as an argument) ...

see the following C++ code, python export code and python test code to see what i mean. I made a simple example which I also tested and it shows the problem I have.

Thank you in advance.
jahn.


More information about the Cplusplus-sig mailing list