When to free memory for C wrapper?

Discussion in 'Python' started by Java and Swing, Oct 13, 2005.

  1. I have been posting lately about writing a C wrapper so Python can
    access my C functions.

    In my wrapper function I have something like...

    static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
    PyObject *data;
    char *result;
    long *d;

    PyArg_ParseTuple(args, "O:wrap_doStuff", &data);

    d = get_long_array(data);

    result = doStuff(d);

    return PyString_FromString(result);
    }

    Now, do I need to use PyMem_Free to free up the "PyObject *data"?

    Thanks.
     
    Java and Swing, Oct 13, 2005
    #1
    1. Advertising

  2. One other thing, I was reading about Py_DECREF...and I see it says

    "A safe approach is to always use the generic operations (functions
    whose name begins with "PyObject_", "PyNumber_", "PySequence_" or
    "PyMapping_"). These operations always increment the reference count of
    the object they return. This leaves the caller with the responsibility
    to call Py_DECREF() when they are done with the result; this soon
    becomes second nature."
    URL: http://www.python.org/doc/api/refcounts.html

    So does that mean before my C wrapper function exits, or returns I
    should Py_DECREF any PyObject's I have? For example, in my previous
    post in this thread..I would have to Py_DECREF(data) right?

    What if in my wrapper I had

    static PyObject *wrap_doStuff(...) {
    PyObject *pyResult;

    ...

    pyResult = PyString_FromString(...);

    return pyResult;
    }

    Is PyResult going to be de-referenced or handled automaticlly by python
    in some way? Or, do I need to somehow call Py_DECREF(pyResult)
    somewhere..if so, where/when?

    Thanks!

    Java and Swing wrote:
    > I have been posting lately about writing a C wrapper so Python can
    > access my C functions.
    >
    > In my wrapper function I have something like...
    >
    > static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
    > PyObject *data;
    > char *result;
    > long *d;
    >
    > PyArg_ParseTuple(args, "O:wrap_doStuff", &data);
    >
    > d = get_long_array(data);
    >
    > result = doStuff(d);
    >
    > return PyString_FromString(result);
    > }
    >
    > Now, do I need to use PyMem_Free to free up the "PyObject *data"?
    >
    > Thanks.
     
    Java and Swing, Oct 13, 2005
    #2
    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. Replies:
    1
    Views:
    834
    Sebastian Millies
    Nov 2, 2006
  2. Replies:
    1
    Views:
    251
  3. Panduranga Chary

    How memory function free() knows how much memory to free.

    Panduranga Chary, Dec 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    428
    Keith Thompson
    Dec 27, 2007
  4. george
    Replies:
    0
    Views:
    1,164
    george
    Aug 29, 2008
  5. mohammed_a_o
    Replies:
    0
    Views:
    302
    mohammed_a_o
    Nov 30, 2010
Loading...

Share This Page