Leaked reference when putting an extension object in a tuple
I appear to be leaking a reference in my C++ extension. The general gist of what I'm trying to achieve is to create a C++ python extension that a) defines a new class (MyClass) b) provides a way for python to register callback functions When a callback becomes necessary, my extension creates a MyClass object, puts it in a tuple and then calls PyEval_CallObject with the callback function and the tuple. Unfortunately it seems to leak references when I put my object in a tuple. The source for a very cut down version of the extension is given below. Running it in a debug build of python gives the following... ActivePython 2.2.1 Build 222 (ActiveState Corp.) based on Python 2.2.1 (#34, Apr 10 2002, 11:35:50) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import PyObjectPasser [8031 refs] PyObjectPasser.RequestCallback() [8034 refs] PyObjectPasser.RequestCallback() [8035 refs] PyObjectPasser.RequestCallback() [8036 refs] PyObjectPasser.RequestCallback() [8037 refs]
Putting an integer into the tuple instead of a MyClass object gives a constant count of 8033 refs. So is there something wrong with a) my code b) python's reference counting c) the boot.python library In the probable case of a), what am I doing wrong? Many thanks in advance Mark code follows... 8<------------------------------------------------------------------- #include <boost/python/class_builder.hpp> #include <boost/python/objects.hpp> struct MyClass { MyClass() { m_priority = 0; } MyClass(int priority) { m_priority; } int m_priority; }; void RequestCallback(); namespace { BOOST_PYTHON_MODULE_INIT(PyObjectPasser) { boost::python::module_builder this_module("PyObjectPasser"); boost::python::class_builder<MyClass> MyClassBuilder(this_module, "MessageInfo"); MyClassBuilder.def(boost::python::constructor<>()); MyClassBuilder.def(boost::python::constructor<int>()); MyClassBuilder.def_readonly(&MyClass::m_priority, "priority"); this_module.def(RequestCallback,"RequestCallback"); } } void RequestCallback() { // create an object of our class MyClass obj(1); // we call a python function in the real code, so build up a tuple // of arguments boost::python::tuple args(1); // put our object in the tuple args.set_item(0,obj); // would now call the callback here } 8<------------------------------------------------------------------- ********************************************************************** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify postmaster@radioscape.com. This footnote also confirms that this email message has been scanned for the presence of computer viruses known at the time of sending. www.radioscape.com **********************************************************************
"Charsley, Mark" <Mark.Charsley@radioscape.com> writes:
I appear to be leaking a reference in my C++ extension. The general gist of what I'm trying to achieve is to create a C++ python extension that a) defines a new class (MyClass) b) provides a way for python to register callback functions
When a callback becomes necessary, my extension creates a MyClass object, puts it in a tuple and then calls PyEval_CallObject with the callback function and the tuple. Unfortunately it seems to leak references when I put my object in a tuple. The source for a very cut down version of the extension is given below. Running it in a debug build of python gives the following...
ActivePython 2.2.1 Build 222 (ActiveState Corp.) based on Python 2.2.1 (#34, Apr 10 2002, 11:35:50) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import PyObjectPasser [8031 refs] PyObjectPasser.RequestCallback() [8034 refs] PyObjectPasser.RequestCallback() [8035 refs] PyObjectPasser.RequestCallback() [8036 refs] PyObjectPasser.RequestCallback() [8037 refs]
Putting an integer into the tuple instead of a MyClass object gives a constant count of 8033 refs. So is there something wrong with a) my code b) python's reference counting c) the boot.python library
In the probable case of a), what am I doing wrong?
Many thanks in advance
Mark
Mark, it doesn't look like you're doing anything wrong, and I'm afraid I can't fix the problem. The Boost Python v1 codebase is going to be retired this week; we're releasing v2, and not making any changes to v1, even for maintenance. If you haven't invested too much in Boost.Python v1, you might try this with v2; I think you'll see better results. Regards, Dave -- David Abrahams * Boost Consulting dave@boost-consulting.com * http://www.boost-consulting.com
participants (2)
-
Charsley, Mark -
David Abrahams