[C++-sig] Boost::Python Reference Counting
laan
larsand at gmail.com
Mon May 19 08:27:39 CEST 2014
Hi,
I'm having issues with Boost::Python objects that contain a reference to a
c++ object:
--- C++ code ----
#include <boost/python.hpp>
struct Container;
struct Element
{
Element(const Container * c) : _container(c) { }
std::string name() const;
const Container * _container;
};
struct Container
{
Container(const std::string & s) : _string(s) { }
Element getElement()
{
return Element(this);
}
std::string name() const
{
return this->_string;
}
std::string _string;
};
std::string Element::name() const
{
return _container->name();
}
using namespace boost::python;
BOOST_PYTHON_MODULE(libkatya)
{
class_<Element>("Element", no_init)
.def("name", &Element::name)
;
class_<Container>("Container", init<std::string>())
.def("getElement", &Container::getElement)
;
}
--- Python Code ----------------------
container = test.Container("X")
elem = container.getElement()
del(container)
print(elem.name())
When calling elem.name(), it will reference the "container" object, which
has been deleted, and I'm getting a segfault.
How can I make boost::python increment the reference counter of Container
when an element containing a pointer to the Container is returned?
--
View this message in context: http://boost.2283326.n4.nabble.com/Boost-Python-Reference-Counting-tp4662504.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.
More information about the Cplusplus-sig
mailing list