From 0a889ceed8cbcc96df35d11760f62f9fb644752a Mon Sep 17 00:00:00 2001 From: Konrad Witaszczyk Date: Wed, 9 Feb 2022 13:13:58 +0000 Subject: [PATCH] Correctly free DTrace resources. Cython's cdef attributes are not regular class attributes as in Python. hasattr(object, name) always returns False for a cdef attribute corresponding to name. Instead, we should simply check a pointer's value. I couldn't confirm it in any documentation but, based on examples I found and my experiments, it seems that a cdef pointer is always initially set to NULL, at least when declared as a class attribute. --- dtrace_cython/consumer.pyx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dtrace_cython/consumer.pyx b/dtrace_cython/consumer.pyx index 8eb9658..c5a6e42 100644 --- a/dtrace_cython/consumer.pyx +++ b/dtrace_cython/consumer.pyx @@ -312,7 +312,7 @@ cdef class DTraceConsumer: self.walk_func = noop_walk if walk_func is None else walk_func cdef int err - if not hasattr(self, 'handle'): + if self.handle == NULL: # ensure we only grab 1 - cython might call init twice, of more. self.handle = dtrace_open(3, 0, &err) if self.handle == NULL: @@ -331,8 +331,9 @@ cdef class DTraceConsumer: """ Release DTrace handle. """ - if hasattr(self, 'handle') and self.handle != NULL: + if self.handle != NULL: dtrace_close(self.handle) + self.handle = NULL cpdef compile(self, str script): """ @@ -437,7 +438,7 @@ cdef class DTraceContinuousConsumer: self.script = script.encode("utf-8") cdef int err - if not hasattr(self, 'handle'): + if self.handle == NULL: # ensure we only grab 1 - cython might call init twice, of more. self.handle = dtrace_open(3, 0, &err) if self.handle == NULL: @@ -456,9 +457,10 @@ cdef class DTraceContinuousConsumer: """ Release DTrace handle. """ - if hasattr(self, 'handle') and self.handle != NULL: + if self.handle != NULL: dtrace_stop(self.handle) dtrace_close(self.handle) + self.handle = NULL cpdef go(self): """