I'm trying out Swig and have the following C code and interface respectively:
// example.c
#include <Python/Python.h>
PyObject *test ( PyObject *self, int i) {
PyObject **x;
x = malloc(sizeof(PyObject *));
*x = PyList_GetItem(self, i);
return *x;
}
// example.i
%module example
%{
/* Put header files here or function declarations like below */
extern PyObject* test(PyObject *self, int i);
%}
extern PyObject* test(PyObject *self, int i);
It compiles properly and I can import the extension module fine. In fact, when I define a variable a to be [{1:1},{2:2}] and do example.test(a, 0) for the first time, it properly returns {1,1}. When I enter a into the Python shell, I get [{1:1},{2:2}] as expected. When I try example.test(a,0) again, I get a segmentation fault. Any ideas why this is happening?
Incref the object before returning it.