Could this C extension be leaking memory?
Sam L.
eghost at flashmail.com
Sat Sep 24 15:32:37 EDT 2005
I am trying this out, parts of it are direct from the Python 2.4
manual.
//----------- icallbk.c
#include "Python.h"
PyObject *fcallback = NULL;
static PyObject*
call_func(PyObject* self, PyObject* args)
{
PyObject *result;
PyObject *arglist;
int arg = 123;
arglist = Py_BuildValue("(i)", arg);
result = PyEval_CallObject(fcallback, arglist);
Py_DECREF(arglist);
Py_DECREF(result);
Py_RETURN_NONE;
}
static PyObject *
set_callback(PyObject *self, PyObject *args)
{
PyObject *result = NULL;
PyObject *temp;
if (PyArg_ParseTuple(args, "O:set_callback", &temp)) {
if (!PyCallable_Check(temp)) {
PyErr_SetString(PyExc_TypeError, "parameter must be
callable");
return NULL;
}
Py_XINCREF(temp); /* Add a reference to new callback */
Py_XDECREF(fcallback); /* Dispose of previous callback */
fcallback = temp; /* Remember new callback */
/* Boilerplate to return "None" */
Py_INCREF(Py_None);
result = Py_None;
}
return result;
}
static PyMethodDef icallbk_methods[] = {
{ "call_func", call_func, METH_VARARGS, "call_func" },
{ "set_callback", set_callback, METH_VARARGS, "set_callback" },
{ 0,0,0,0 }
};
PyMODINIT_FUNC
initicallbk(void)
{
(void) Py_InitModule("icallbk", icallbk_methods);
}
"""
I use the following code to run the extension above.
"""
# cb.py: Code to run the extension above.
import icallbk as c
def f(a): print a
c.set_callback(f)
c.call_func()
"""
But when I run that for a hundred times, I think the system is losing
100KB or thereabouts that is no longer re-claimed. Can someone confirm
this, and if it so, kindly inform me what is wrong with the code?
Thanks
More information about the Python-list
mailing list