When to free memory for C wrapper?

J

Java and Swing

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.
 
J

Java and Swing

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!
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top