diff --git a/src/calibre/devices/mtp/windows/device.cpp b/src/calibre/devices/mtp/windows/device.cpp index 45914d0268..b55592eead 100644 --- a/src/calibre/devices/mtp/windows/device.cpp +++ b/src/calibre/devices/mtp/windows/device.cpp @@ -22,8 +22,7 @@ dealloc(Device* self) Py_END_ALLOW_THREADS; } - Py_XDECREF(self->device_information); self->device_information = NULL; - + self->device_information.release(); Py_TYPE(self)->tp_free((PyObject*)self); } @@ -37,7 +36,7 @@ init(Device *self, PyObject *args, PyObject *kwds) if (client_information) { self->device = open_device(self->pnp_id.ptr(), client_information); if (self->device) { - self->device_information = get_device_information(self->device, self->bulk_properties); + self->device_information.attach(get_device_information(self->device, self->bulk_properties)); if (self->device_information) ret = 0; } } @@ -53,7 +52,7 @@ update_data(Device *self, PyObject *args) { CComPtr bulk_properties; di = get_device_information(self->device, bulk_properties); if (di == NULL) return NULL; - Py_XDECREF(self->device_information); self->device_information = di; + self->device_information.attach(di); Py_RETURN_NONE; } // }}} @@ -171,7 +170,8 @@ static PyMethodDef Device_methods[] = { // Device.data {{{ static PyObject * Device_data(Device *self, void *closure) { - Py_INCREF(self->device_information); return self->device_information; + PyObject *ans = self->device_information.ptr(); + Py_INCREF(ans); return ans; } // }}} diff --git a/src/calibre/devices/mtp/windows/global.h b/src/calibre/devices/mtp/windows/global.h index 4e92691713..6f70f0fe06 100644 --- a/src/calibre/devices/mtp/windows/global.h +++ b/src/calibre/devices/mtp/windows/global.h @@ -42,7 +42,7 @@ typedef struct { // Type-specific fields go here. wchar_raii pnp_id; CComPtr device; - PyObject *device_information; + pyobject_raii device_information; CComPtr bulk_properties; } Device; extern PyTypeObject DeviceType;