Could this C extension be leaking memory?

S

Sam L.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top