I try to make an extension in C for Python. I've never done this before. Regardless of what the function in C will do, the problem is as follows. I use malloc to allocate memory. But when I use the free() function
setup.py:
from distutils.core import setup, Extension
setup(name='rpd', version='1.0',  \
      ext_modules=[Extension('rpd', ['RPB.c'])])
I install the module with: python setup.py install. But then after I type:
import rpd
rpd.rpd("No reason to give string")
the IDLE GUI freezes and restarts. And I don't see not calling free() on allocated memory as an option. Any help would be appreciated.
EDIT:
So I managed to run the previous version of code, although I didn't make any dramatic changes. And I can't still figure out why yesterday the same code wouldn't work. Then I wanted to make a step towards the full C code I have. So I added a function in RPB.c code and some minor changes. You can see the updated code below.. but now, I got the same problem again :/
RPB.c:
 #include "Python.h"
 char** tokenize(const char* input)
 {
    char* str = strdup(input);
    int count = 0;
    int capacity = 8;
    char** result = malloc(capacity*sizeof(*result));
    char* tok=strtok(str," ");
    while(1)
    {
        if (count >= capacity)
            result = realloc(result, (capacity*=2)*sizeof(*result));
        result[count++] = tok? strdup(tok) : tok;
        if (!tok) break;
        tok=strtok(NULL," ");
    }
    // When this line is executed... b00m!
    free(str);
    return result;
}
static PyObject* rpd(PyObject *self, PyObject *args){
    char *filename, **test;
    if (!PyArg_ParseTuple(args, "s", &filename)) {
        return Py_BuildValue("s", "Error during reading passed parameters!");
    }
    // test = malloc(strlen(filename)+1);
    test = tokenize(filename);
    // This is never returned due to free in tokenize - placed here for debug purposes
    return Py_BuildValue("s", "What again?!");
    // When this line is executed... b00m!
    //free(test);
   
    // This is never returned, because program crashes
    //return Py_BuildValue("s", "Everything ok");
   
}
static char rpd_docs[] ="rpd(c): Give a filename!\n";
static PyMethodDef rpd_funcs[] = {
        {"rpd", (PyCFunction)rpd,
         METH_VARARGS, rpd_docs},
        {NULL, NULL, 0, NULL}
};
void initrpd(void)
{
      Py_InitModule3("rpd", rpd_funcs, "Give a filename!!");
}
 
     
    