22 PyObject * 23 refcount_demo(PyObject *self, PyObject *args) 24 { 25 PyObject *list; 26 PyObject *item; 27 list = PyList_New(1); 28 if (!list) 29 return NULL; 30 31 item = PyLong_FromLong(42); 32 if (!item) 33 return NULL; 34 35 PyList_SetItem(list, 0, item); 36 return list; 37 } 38 39 static PyMethodDef test_methods[] = { 40 {"refcount_demo", refcount_demo, METH_VARARGS, NULL}, 41 {NULL, NULL, 0, NULL} /* Sentinel */ 42 };

[david@fedora-15 gcc-plugin]$ ./gcc-with-python cpychecker.py -I/usr/include/python2.7 refcount-demo.c

refcount-demo.c: In function ‘refcount_demo’: refcount-demo.c:37:1: error: ob_refcnt of PyListObject is 1 too high refcount-demo.c:27:10: note: PyListObject allocated at: list = PyList_New(1); refcount-demo.c:27:10: note: when PyList_New() succeeds at: list = PyList_New(1); refcount-demo.c:28:8: note: when taking False path at: if (!list) refcount-demo.c:31:10: note: reaching: item = PyLong_FromLong(42); refcount-demo.c:31:10: note: when PyLong_FromLong() fails at: item = PyLong_FromLong(42); refcount-demo.c:32:8: note: when taking True path at: if (!item) refcount-demo.c:33:9: note: reaching: return NULL; refcount-demo.c:37:1: note: when returning