Boost logo

Boost Users :

Subject: [Boost-users] [Boost.Python] Using Weak Ptrs with Boost Python?
From: Dan Bailey (drb_at_[hidden])
Date: 2009-10-26 09:59:15


Trying to set up a dependency in C++ with a parent-child relationship.
The parent contains the child and the child has a weak pointer to the
parent.

I would also like to be able to derive from the parent in Python.
However, when I do this, I get a weak pointer error connecting up this
parent-child relationship.

C++ code:

#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

using namespace boost;
using namespace boost::python;

struct Child;

struct Parent : public enable_shared_from_this<Parent>
{
     void initialize();
     shared_ptr<Child> m_child;
};

struct Child: public enable_shared_from_this<Child>
{
     void setParent(shared_ptr<Parent> ptr);
     weak_ptr<Parent> m_parent;
};

void Parent::initialize()
{
     shared_ptr<Child> ptr(new Child);
     m_child = ptr;

     m_child->setParent(shared_from_this());
}

void Child::setParent(shared_ptr<Parent> ptr)
{
     m_parent = ptr;
}

static PyObject* create(PyObject* object)
{
     PyObject* instance = PyObject_CallObject(object, NULL);

     Parent* parent = extract<Parent*>(instance);
     parent->initialize();

     return instance;
}

Python binding:

BOOST_PYTHON_MODULE(test_module)
{
     class_<Parent>("Parent");

     def("create", &create);
}

Python code:

from test_module import *

class Test(Parent):
     def __init__(self):
         Parent.__init__(self)

n = create(Test)

Error:

Traceback (most recent call last):
   File "main.py", line 8, in <module>
     n = create(Test)
RuntimeError: tr1::bad_weak_ptr

If I try and convert the extracted pointer to Parent into a shared_ptr,
I get a free() invalid pointer error in Python.

Is there a way of getting round this problem or should I give up using
weak pointers with Boost Python?


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net