私はSwigを試していて、それぞれ次のCコードとインターフェースを持っています:
// 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);
適切にコンパイルされ、拡張モジュールを正常にインポートできます。実際、変数 a を [{1:1},{2:2}] と定義して、初めて example.test(a, 0) を実行すると、適切に {1,1} が返されます。a を Python シェルに入力すると、期待どおり [{1:1},{2:2}] が返されます。example.test(a,0) を再試行すると、セグメンテーション違反が発生します。なぜこれが起こっているのですか?