strange thing after call PyObject_CallMethod

"Martin v. Löwis" martin at
Sat Jan 3 19:00:18 CET 2009

> Why this happenning and who makes Py_INCREF(self)?

There are multiple possible explanations, but I think you
have ruled out most of them:

1. on_recv might be returning self. So py_result would be
   the same as self, and hence be an additional reference.
   However, you said that on_recv raised an exception, so
   py_result should be NULL (can you confirm?)

2. there might be a reference leak in the implementation of
   on_recv. However, you say that it is all fine at the
   end, so this is unlikely

3. The implementation of on_recv stores the object inside
   another objects. There are too many possibilities for that
   to enumerate; here are some examples:

   def on_recv(self, buf):
       global foo, bar
       foo = self          # creates global reference
       bar = self.on_send  # creates bound method = self     # creates cyclic reference
       foobar.append(self) # adds self into container

If you are using gdb, I recommend to set a watchpoint on
changes to ob_refcnt:

(gdb) p &((PyObject*)self)->ob_refcnt
$1 = (Py_ssize_t *) 0xa0cb5a0
(gdb) watch *(Py_ssize_t *) 0xa0cb5a0
Hardware watchpoint 2: *(ssize_t *) 168605088
(gdb) c
Hardware watchpoint 2: *(ssize_t *) 168605088

Old value = 2
New value = 1
0xb7d5f406 in list_clear (a=0xa041c74) at Objects/listobject.c:550
550                             Py_XDECREF(item[i]);

As you can see: this specific object was stored in a list, and
the list is now being cleared.


More information about the Python-list mailing list