Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Could this C extension be leaking memory?

Reply
Thread Tools

Could this C extension be leaking memory?

 
 
Sam L.
Guest
Posts: n/a
 
      09-24-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Leaking memory when writing to URL Simon Andrews Java 6 01-06-2006 02:53 PM
huge recursive and memory leaking? jojo Java 5 09-08-2005 11:39 AM
Leaking Code MattC ASP .Net 4 02-21-2005 03:10 PM
NAT leaking? Loryan Strant Cisco 1 02-09-2004 11:39 PM
Leaking IP info? mb Computer Security 0 09-25-2003 07:18 PM



Advertisments