According to the documentation , the third argument to PyCapsule_New() can specify a destructor, which I believe should be called when the capsule is destroyed.
void mapDestroy(PyObject *capsule) { lash_map_simple_t *map; fprintf(stderr, "Entered destructor\n"); map = (lash_map_simple_t*)PyCapsule_GetPointer(capsule, "MAP_C_API"); if (map == NULL) return; fprintf(stderr, "Destroying map %p\n", map); lashMapSimpleFree(map); free(map); } static PyObject * mapSimpleInit_func(PyObject *self, PyObject *args) { unsigned int w; unsigned int h; PyObject *pymap; lash_map_simple_t *map = (lash_map_simple_t*)malloc(sizeof(lash_map_simple_t)); pymap = PyCapsule_New((void *)map, "MAP_C_API", mapDestroy); if (!PyArg_ParseTuple(args, "II", &w, &h)) return NULL; lashMapSimpleInit(map, &w, &h); return Py_BuildValue("O", pymap); }
However, when I instantiate an object and delete it or exit the Python console, the destructor does not seem to be called:
>>> a = mapSimpleInit(10,20) >>> a <capsule object "MAP_C_API" at 0x7fcf4959f930> >>> del(a) >>> a = mapSimpleInit(10,20) >>> a <capsule object "MAP_C_API" at 0x7fcf495186f0> >>> quit() la sh@CANTANDO ~/programming/src/liblashgame $
I assume that it has something to Py_BuildValue() with Py_BuildValue() , returning a new link to the "capsule", which when deleted does not affect the original. Anyway, how can I ensure that the object is properly destroyed?
Using Python 3.4.3 [GCC 4.8.4] (on Linux)
source share