Reference Counts & Extensions..

Courageous jkraska1 at
Sat Jun 16 15:00:07 EDT 2001

>No, you must Py_INCREF(pyObj). You cannot return borrowed references;
>the interpreter will decref your result when it is not longer needed,
>releasing your object. This will make pyObj an invalid pointer.

To the OP:

BTW, it can be somewhat helpful to print the refcnt of an object when
you return it from one of your modules as you are learning this stuff.
Keep in mind that the number is always one higher than you'd expect,
as Python has to hold an additional reference to it in order to print it.

IOW, when you expect a refcnt of 1, look for 2, and so forth.

After doing this a bit, you should grok the refcnt mechanism properly.


More information about the Python-list mailing list