C Wrapper Function, crashing Python?
Tony Nelson
*firstname*nlsnews at georgea*lastname*.com
Sat Oct 15 00:56:06 EDT 2005
In article <1129122788.882046.4270 at o13g2000cwo.googlegroups.com>,
"Java and Swing" <codecraig at gmail.com> wrote:
> one more update...
>
> if I remove PyMem_Free and free(...) ...so no memory clean up...I can
> still only call doStuff 4 times, the 5th attemp crashes Python.
>
> Java and Swing wrote:
> > update:
> > if I use C's free(result), free(a) free(b) instead of PyMem_Free...I
> > only get one successfuly use/call of doStuff.
> >
> > i.e.
> > // this works
> > doStuff(...)
> >
> > // python crashes here
> > doStuff(...)
> >
> > Java and Swing wrote:
> > > static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
> > > // this will store the result in a Python object
> > > PyObject *finalResult;
> > >
> > > // get arguments from Python
> > > char *result = 0;
> > > char *in= 0;
> > > char *aString = 0;
> > > char *bString = 0;
> > > MY_NUM *a;
> > > MY_NUM *b;
> > > int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
> > > if (!ok) return 0;
> > >
> > > // do work to get a and b
> > > // count - returns an int; GetVal - returns a char *
> > > a = GetVal(aString, count(aString, ","));
> > > b = GetVal(bString, count(bString, ","));
> > >
> > > // make function call, which returns a char *
> > > result = doStuff(in, a, b);
> > >
> > > // save result in Python string
> > > finalResult = PyString_FromString(result);
> > >
> > > // free memory
> > > PyMem_Free(result);
> > > PyMem_Free(a);
> > > PyMem_Free(b);
> > >
> > > // return the result as a Python string
> > > return finalResult;
> > > }
> > >
> > > ...from python I can call this function 4 times...works fine. WHen I
> > > call it for the fifth time python.exe crashes. im thinking some memory
> > > problem in the wrapper function perhaps...but I am not sure. The
> > > actually C function, doStuff can be called 5, 6,7...N times without a
> > > problem
> > > so i know its gotta be my wrapper.
> > >
> > > Any ideas? Thanks!
>
I think your wrapper should look something like:
static PyObject *wrap_doStuff(PyObject *self, PyObject *args)
{
// this will store the result in a Python object
PyObject *finalResult;
// get arguments from Python
char *result = 0;
char *in= 0;
char *aString = 0;
char *bString = 0;
MY_NUM *a;
MY_NUM *b;
int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
if (!ok) return 0;
// do work to get a and b
// count - returns an int; GetVal - returns a char *
a = GetVal(aString, count(aString, ","));
b = GetVal(bString, count(bString, ","));
// make function call, which returns a char *
result = doStuff(in, a, b);
// save result in Python string
finalResult = PyString_FromString(result);
// free memory
free(result);
free(a);
free(b);
// return the result as a Python string
return finalResult;
}
You must match malloc() with free(), and PyMem_Malloc() with
PyMem_Free(). Malloc() and free() usually crash /after/ the call that
did the damage. You may wish to avail yourself of your platform's
malloc debugging facilities.
Note that I don't do this stuff in C, I do it in pyrex, and I'm new to
it anyway, so there may still be something wrong. Unless you are
determined to learn how to do this in C, I think you should switch to
pyrex.
________________________________________________________________________
TonyN.:' *firstname*nlsnews at georgea*lastname*.com
' <http://www.georgeanelson.com/>
More information about the Python-list
mailing list