C Wrapper Function, crashing Python?

Java and Swing codecraig at gmail.com
Wed Oct 12 09:09:33 EDT 2005


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!




More information about the Python-list mailing list