[C++-sig] Reference leak in life_support.cpp

Raoul Gough RaoulGough at yahoo.co.uk
Tue Sep 16 17:54:55 CEST 2003


I've been checking for reference leaks using python_d, and I think
I've found one using return_internal_reference where the return value
is a null pointer (or even just void). This gets returned to Python as
Py_None, and there is a special case in life_support.cpp to handle it:

    if (nurse == Py_None)
        return incref(nurse);

In my testing, this leaks a reference to Py_None, since the counter
already got incremented in the boost::python::detail::none() call that
originally introduced the Py_None pointer. This shows up as an
increasing total ref count using python_d when invoking the following
function:

IntWrapper const *foo (IntWrapper const &i) {
  return 0;
}

via a Python extension module with

  def ("foo", foo, return_internal_reference<>());

e.g.

>>> foo(IntWrapper(3))
[8453 refs]
>>> foo(IntWrapper(3))
[8454 refs]
>>> foo(IntWrapper(3))
[8455 refs]
>>> foo(IntWrapper(3))
[8456 refs]
>>> foo(IntWrapper(3))
[8457 refs]
>>> foo(IntWrapper(3))
[8458 refs]

[etc...]

It also does the same for a function with void return type and
return_internal_reference (OK, OK, I know that's weird, but it
happened).

http://mail.python.org/pipermail/c++-sig/2002-August/001940.html has
some previous discussion on this issue.

-- 
Raoul Gough.
(setq dabbrev-case-fold-search nil)





More information about the Cplusplus-sig mailing list