Skip to content

Commit

Permalink
Increase reference to the image while capsule is alive
Browse files Browse the repository at this point in the history
  • Loading branch information
homm committed Sep 16, 2024
1 parent d8ef314 commit bc97369
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -3714,9 +3714,18 @@ _getattr_id(ImagingObject *self, void *closure) {
return PyLong_FromSsize_t((Py_ssize_t)self->image);
}

static void
_ptr_destructor(PyObject *capsule) {
PyObject *self = (PyObject *)PyCapsule_GetContext(capsule);
Py_DECREF(self);
}

static PyObject *
_getattr_ptr(ImagingObject *self, void *closure) {
return PyCapsule_New(self->image, IMAGING_MAGIC, NULL);
PyObject *capsule = PyCapsule_New(self->image, IMAGING_MAGIC, _ptr_destructor);
Py_INCREF(self);
PyCapsule_SetContext(capsule, self);
return capsule;
}

static PyObject *
Expand Down

0 comments on commit bc97369

Please sign in to comment.