I’m using Boost.Python to expose my C++ code to Python. I’ve encountered a difficulty related to having an object passed from one language to the other multiple times. Here’s what I want to do:
The C++ code
class Base
{
public:
void baseTest() {
std::cout << "Base::basetest()";
}
};
class Deriv
: public Base
{
public:
void derivTest() {
std::cout << "Deriv::derivTest()";
}
};
void call(Base& b, boost::python::object func)
{
func(b);
}
BOOST_PYTHON_MODULE(event)
{
using namespace boost;
using namespace boost::python;
class_<Base>("Base")
.def("baseTest", &Base::baseTest)
;
class_<Deriv, bases<Base>>("Deriv")
.def("derivTest", &Deriv::derivTest)
;
def("call", call);
}
Python code
from event import *
def callback(deriv):
deriv.baseTest() # works fine
deriv.derivTest() # crash!
def run():
d = Deriv()
call(d, callback)
What I want to happen
-
C++: Calls the
run()function defined in Python. (No problem here) -
Python:
run()creates a newDerivobject; it passes it and a function object,callback, back to C++, through thecallfunction. (Also okay) -
C++:
call()takes theDerivobject as aBase&. It passes the Base-reference to the Python callback it received through the second parameter. -
Python: The callback receives the
Baseobject from C++. However, it expects it to be aDeriv: if I callderivTest()the program crashes. However, if I callbaseTest(), it doesn’t crash.
How can I make the callback not crash?
Thanks for the comments, I found the solution to this. It’s actually quite simple, you just have to wrap the Base object in a shared_ptr instead of passing it by reference, like this:
But be careful about something. I tried to used std::shared_ptr that comes with Visual C++ 2010 Express (the ‘memory’ header) and it caused a crash. I had to use the boost::shared_ptr for it to work. (I’m using version 1.46.1 of Boost.)