Reference Count Confusion

Douglas Zongker dougz at cs.washington.edu
Tue Jun 25 18:08:34 EDT 2002


Jeff Smith <smithj at kryos.colorado.edu> wrote:
: I'm a little puzzled about reference counts from Python objects created
: in a C extension. [...]
:
: static PyObject *clean(PyObject *self, PyObject *args)
: {
:   PyArrayObject *c;
:
:   PyArg_ParseTuple(args, "O", &c);
:
:   printf("clean: %d\n", c->ob_refcnt);
:   if (c->data != NULL) free(c->data);
:   if (c->dimensions != NULL) free(c->dimensions);
:   if (c->strides == NULL) free(c->strides);
:   Py_DECREF(c);
:   printf("clean: %d\n", c->ob_refcnt);
:
:   return Py_BuildValue("");
: }

You shouldn't decref c here.  When you use the "O" specifier with
PyArg_ParseTuple(), it gives you a borrowed reference to the object.
You're losing references each time you call this function, and that's
probably messing things up.

dz







More information about the Python-list mailing list