[C++-sig] Transfering ownership from python to C++
Shawn McGrath
shawn.mcgrath at gmail.com
Mon Mar 26 11:09:14 CEST 2007
Hi guys, I've been working on this problem for at least a week now,
and I can't find a solution. I'm pretty much ready to give up this
project if I can't fix this.
In C++ I have a base class called Entity with two pure virtual
functions: update and process which are exposed to python. I subclass
Entity in python, and pass instances of the subclassed entities to a
C++ function called "addEntity" defined like this:
void pyAddEntity(const std::string &name, PyObject *e)
{
pyEntity *ent = boost::python::extract<pyEntity *>(e);
assert(ent != NULL);
EntityID eid = Engine::Instance().addEntity(ent, name);
try
{
ent->self = e;
ent->added = true;
Py_XINCREF(ent->self);
}
catch (...)
{
handleError();
LOG("ERROR PY_XINCREF");
}
}
To kill the memory, I call pyEntity::kill() which sets a done flag to
true, and the Engine deletes it. I tried putting Py_XDECREF(self)
inside pyEntity's dtor and it crashes with the stacktrace showing
ntdll, (so meaningless). I tried delaying the call to Py_XDECREF
until after the gc has run, still no good. There's nothing I can do
to call Py_XDECREF().
The delayed delete stacktrace is a little more meaningful, it shows
(unknown frames removed):
#1 007C34B0 std::auto_ptr<rsblsb::python::pyEntity>::~auto_ptr(this=0xabdeee8)
(C:/MinGW/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/memory:258)
#2 006E126B boost::python::objects::pointer_holder<std::auto_ptr<rsblsb::python::pyEntity>,
rsblsb::python::pyEntity>::~pointer_holder(this=0xabdeee0)
(C:/Game/svn/Platform/Framework/Python/Modules/PyModuleEntity.cc:127)
#3 600CD32E boost_python-mgw-1_33_1!_ZN5boost6python15instance_holder10deallocateEP7_objectPv()
(C:\Game\svn\Platform\Output\boost_python-mgw-1_33_1.dll:??)
#4 1E0AB496 python24!PyType_GenericNew() (C:\WINDOWS\system32\python24.dll:??)
#10 0053945B rsblsb::python::InterpreterClass::process(this=0x5356f44d)
(C:/Game/svn/Platform/Framework/Python/PythonInterpreter.cc:92)
#17 6011E19C boost_python-mgw-1_33_1!_ZNK5boost6python6detail12wrapper_base12get_overrideEPKcP11_typeobject()
(C:\Game\svn\Platform\Output\boost_python-mgw-1_33_1.dll:??)
I tried changing the addEntity() function like so: void
pyAddEntity(const std::string &name, std::auto_ptr<pyEntity> e);
But that doesn't allow for virtual functions to be defined in Python,
so that's not an option.
Is there anything else I can try? I *REALLY* don't want to give up on
this project, but I can't handle these memory leaks.
Thanks a lot for the help
-Shawn.
More information about the Cplusplus-sig
mailing list