Could this C extension be leaking memory?

Discussion in 'Python' started by Sam L., Sep 24, 2005.

  1. Sam L.

    Sam L. Guest

    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
     
    Sam L., Sep 24, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jojo
    Replies:
    5
    Views:
    378
    jan V
    Sep 8, 2005
  2. Simon Andrews

    Leaking memory when writing to URL

    Simon Andrews, Jan 6, 2006, in forum: Java
    Replies:
    6
    Views:
    3,255
    Robert Klemme
    Jan 6, 2006
  3. Sigmathaar

    Memory leaking

    Sigmathaar, Dec 20, 2005, in forum: C++
    Replies:
    6
    Views:
    599
    Axter
    Dec 20, 2005
  4. zl2k
    Replies:
    6
    Views:
    758
    mlimber
    Apr 3, 2006
  5. gNash

    Memory leaking..

    gNash, Nov 15, 2007, in forum: C Programming
    Replies:
    25
    Views:
    779
    J. J. Farrell
    Nov 16, 2007
Loading...

Share This Page