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

David Abrahams dave at boost-consulting.com
Mon Oct 7 20:10:02 CEST 2002


"Charsley, Mark" <Mark.Charsley at 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 at boost-consulting.com * http://www.boost-consulting.com





More information about the Cplusplus-sig mailing list