Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > When to free memory for C wrapper?

Reply
Thread Tools

When to free memory for C wrapper?

 
 
Java and Swing
Guest
Posts: n/a
 
      10-13-2005
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.

 
Reply With Quote
 
 
 
 
Java and Swing
Guest
Posts: n/a
 
      10-13-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.


 
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
How memory function free() knows how much memory to free. Panduranga Chary C Programming 2 12-27-2007 06:01 AM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM



Advertisments