[C++-sig] Leaked reference when putting an extension object in a tuple

Charsley, Mark Mark.Charsley at radioscape.com
Mon Oct 7 16:28:10 CEST 2002


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 at 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
**********************************************************************





More information about the Cplusplus-sig mailing list