C Wrapper Function, crashing Python?

Java and Swing codecraig at gmail.com
Wed Oct 12 10:40:37 EDT 2005


ok, further digging...I found that in the C function GetVal...it is
crashing where I try to malloc some memory. Note, I have no problems
when running this from C..just from Python using my wrapper.

GetVal looks something like..
MY_NUM *GetVal(const char *in, const int x) {
    MY_NUM *results, *returnResults;
    results = (MY_NUM *) malloc((x * sizeof(MY_NUM) + 1);
    returnResults = results;
    // ..do more work..
    return returnResults;
}

I put in print statements into the C code and found that it is crashing
at the line where I malloc some space for "results".

...any ideas why this is crashing when calling from Python via C
wrapper?

Java and Swing wrote:
> Sorry about the double post...
>
> anyhow, after putting in debug statements I found that it was crashing
> when it called, free(result)....so I removed the free(result).
>
> now it crashes when it gets to, b = GetVal(bString, count(bString,
> ","));
>
> ..any ideas?
>
> Java and Swing wrote:
> > Antoon,
> >    I just saw that to.  I updated the code like so...
> >
> > 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 *) PyMem_Malloc((20 * sizeof(MY_NUM) + 1);  //
> > the array will be 20 long
> >   	MY_NUM *b = (MY_NUM *) PyMem_Malloc((20 * sizeof(MY_NUM) + 1); //
> > the array will be 20 long
> >   	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 MY_NUM * (a pointer
> > to a MY_NUM array)
> >   	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(a);
> >   	PyMem_Free(b);
> > 	free(aString);
> >  	free(bString);
> > 	free(result);
> >
> >   	// return the result as a Python string
> >   	return finalResult;
> >   }
> >
> > ..as you can see, i malloc'ed memory, and free'd the memory.  However,
> > I still have python crashing (after only 3 successful calls to
> > doStuff).  And yes, doStuff is a plain C function...nothing related to
> > Python.
> >
> >
> > Antoon Pardon wrote:
> > > Op 2005-10-12, Java and Swing schreef <codecraig at gmail.com>:
> > > > 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!
> > >
> > > Well assuming your doStuff is a C function that knows nothing of python.
> > > it might be the PyMem_Free(result).
> > > http://docs.python.org/api/memoryInterface.html says the following:
> > >
> > > void PyMem_Free(void *p)
> > >     Frees the memory block pointed to by p, which must have been
> > >     returned by a previous call to PyMem_Malloc() or PyMem_Realloc().
> > >     Otherwise, or if PyMem_Free(p) has been called before, undefined
> > >     behavior occurs. If p is NULL, no operation is performed.
> > >
> > > But your result wasn't allocated by a PyMem_Malloc, it was returned
> > > to you by a C function.
> > > 
> > > -- 
> > > Antoon Pardon




More information about the Python-list mailing list