When to free memory for C wrapper?
Java and Swing
codecraig at gmail.com
Thu Oct 13 09:47:06 EDT 2005
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.
More information about the Python-list
mailing list