Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Boost Python with Weak Ptrs?
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>C++ code:</p> <pre><code>#include &lt;boost/python.hpp&gt; #include &lt;boost/shared_ptr.hpp&gt; #include &lt;boost/enable_shared_from_this.hpp&gt; using namespace boost; using namespace boost::python; struct Child; struct Parent : public enable_shared_from_this&lt;Parent&gt; { void initialize(); shared_ptr&lt;Child&gt; m_child; }; struct Child: public enable_shared_from_this&lt;Child&gt; { void setParent(shared_ptr&lt;Parent&gt; ptr); weak_ptr&lt;Parent&gt; m_parent; }; void Parent::initialize() { shared_ptr&lt;Child&gt; ptr(new Child); m_child = ptr; m_child-&gt;setParent(shared_from_this()); } void Child::setParent(shared_ptr&lt;Parent&gt; ptr) { m_parent = ptr; } static PyObject* create(PyObject* object) { PyObject* instance = PyObject_CallObject(object, NULL); Parent* parent = extract&lt;Parent*&gt;(instance); parent-&gt;initialize(); return instance; } </code></pre> <p>Python binding:</p> <pre><code>BOOST_PYTHON_MODULE(test_module) { class_&lt;Parent&gt;("Parent"); def("create", &amp;create); } </code></pre> <p>Python code:</p> <pre><code>from test_module import * class Test(Parent): def __init__(self): Parent.__init__(self) n = create(Test) </code></pre> <p>Error:</p> <pre><code>Traceback (most recent call last): File "main.py", line 8, in &lt;module&gt; n = create(Test) RuntimeError: tr1::bad_weak_ptr </code></pre> <p>If I try and convert the extracted pointer to Parent into a shared_ptr, I get a free() invalid pointer error in Python.</p> <p>Is there a way of getting round this problem or should I give up using weak pointers with Boost Python?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload