From 2af7b2adbe47cff9aef31c098014de05f5267cb7 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Tue, 12 Jul 2016 13:24:56 -0400 Subject: [PATCH] Correctly dealloc detached handlers This is a different fix for issue #37. The original fix that removed dynamic memory allocation of uv_handles had to be reverted. --- uvloop/handles/handle.pxd | 2 +- uvloop/handles/handle.pyx | 13 ++++++------- uvloop/handles/process.pyx | 4 +--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/uvloop/handles/handle.pxd b/uvloop/handles/handle.pxd index e60c52c..39805fb 100644 --- a/uvloop/handles/handle.pxd +++ b/uvloop/handles/handle.pxd @@ -17,7 +17,7 @@ cdef class UVHandle: cdef _error(self, exc, throw) cdef _fatal_error(self, exc, throw, reason=?) - cdef _free(self) + cdef inline _free(self) cdef _close(self) diff --git a/uvloop/handles/handle.pyx b/uvloop/handles/handle.pyx index 71a3ea8..805f06e 100644 --- a/uvloop/handles/handle.pyx +++ b/uvloop/handles/handle.pyx @@ -70,8 +70,7 @@ cdef class UVHandle: self._closed = 1 self._free() - - cdef _free(self): + cdef inline _free(self): PyMem_Free(self._handle) self._handle = NULL @@ -303,17 +302,17 @@ cdef void __uv_close_handle_cb(uv.uv_handle_t* handle) with gil: PyMem_Free(handle) return - if handle.data is not __NOHANDLE__: + if handle.data is __NOHANDLE__: + # The original UVHandle is long dead. Just free the mem of + # the uv_handle_t* handler. + PyMem_Free(handle) + else: h = handle.data - h._handle = NULL IF DEBUG: h._loop._debug_handles_closed.update([ h.__class__.__name__]) h._free() Py_DECREF(h) # Was INCREFed in UVHandle._close - return - - PyMem_Free(handle) cdef void __close_all_handles(Loop loop): diff --git a/uvloop/handles/process.pyx b/uvloop/handles/process.pyx index d8c3107..6da0b1c 100644 --- a/uvloop/handles/process.pyx +++ b/uvloop/handles/process.pyx @@ -177,7 +177,7 @@ cdef class UVProcess(UVHandle): 'UVProcess._close_after_spawn called after uv_spawn') self._fds_to_close.add(fd) - cdef _free(self): + def __dealloc__(self): if self.uv_opt_env is not NULL: PyMem_Free(self.uv_opt_env) self.uv_opt_env = NULL @@ -186,8 +186,6 @@ cdef class UVProcess(UVHandle): PyMem_Free(self.uv_opt_args) self.uv_opt_args = NULL - UVHandle._free(self) - cdef char** __to_cstring_array(self, list arr): cdef: int i