C Wrapper Function, crashing python

8 views
Skip to first unread message

jpy

unread,
Oct 12, 2005, 11:33:00 AM10/12/05
to python
I have written a wrapper function so that Python can call my C
function. from python I can call this function 4 times...works fine.
When I
call it for the fifth time python.exe crashes.

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); -- this crashed it

// return the result as a Python string
return finalResult;
}

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?

Reply all
Reply to author
Forward
0 new messages