Skip to content

Commit 0a889ce

Browse files
committed
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.
1 parent a2c394a commit 0a889ce

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

dtrace_cython/consumer.pyx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ cdef class DTraceConsumer:
312312
self.walk_func = noop_walk if walk_func is None else walk_func
313313

314314
cdef int err
315-
if not hasattr(self, 'handle'):
315+
if self.handle == NULL:
316316
# ensure we only grab 1 - cython might call init twice, of more.
317317
self.handle = dtrace_open(3, 0, &err)
318318
if self.handle == NULL:
@@ -331,8 +331,9 @@ cdef class DTraceConsumer:
331331
"""
332332
Release DTrace handle.
333333
"""
334-
if hasattr(self, 'handle') and self.handle != NULL:
334+
if self.handle != NULL:
335335
dtrace_close(self.handle)
336+
self.handle = NULL
336337

337338
cpdef compile(self, str script):
338339
"""
@@ -437,7 +438,7 @@ cdef class DTraceContinuousConsumer:
437438
self.script = script.encode("utf-8")
438439

439440
cdef int err
440-
if not hasattr(self, 'handle'):
441+
if self.handle == NULL:
441442
# ensure we only grab 1 - cython might call init twice, of more.
442443
self.handle = dtrace_open(3, 0, &err)
443444
if self.handle == NULL:
@@ -456,9 +457,10 @@ cdef class DTraceContinuousConsumer:
456457
"""
457458
Release DTrace handle.
458459
"""
459-
if hasattr(self, 'handle') and self.handle != NULL:
460+
if self.handle != NULL:
460461
dtrace_stop(self.handle)
461462
dtrace_close(self.handle)
463+
self.handle = NULL
462464

463465
cpdef go(self):
464466
"""

0 commit comments

Comments
 (0)