From 2acf44596a4f85a2423060300ed4a4b503532b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sun, 18 Dec 2016 19:39:17 +0100 Subject: [PATCH] Avoid possible dangling objects referenced by timer callback delegates. Fixes #2. --- source/eventcore/drivers/timer.d | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/source/eventcore/drivers/timer.d b/source/eventcore/drivers/timer.d index a2b83ae..be73d4c 100644 --- a/source/eventcore/drivers/timer.d +++ b/source/eventcore/drivers/timer.d @@ -15,6 +15,7 @@ final class LoopTimeoutTimerDriver : EventDriverTimers { import std.datetime : Clock; import std.range : SortedRange, assumeSorted, take; import core.time : hnsecs, Duration; + import core.memory : GC; private { static FreeList!(Mallocator, TimerSlot.sizeof) ms_allocator; @@ -84,6 +85,7 @@ final class LoopTimeoutTimerDriver : EventDriverTimers { TimerSlot* tm; try tm = ms_allocator.make!TimerSlot; catch (Exception e) return TimerID.invalid; + GC.addRange(tm, TimerSlot.sizeof, typeid(TimerSlot)); assert(tm !is null); tm.id = id; tm.refCount = 1; @@ -167,7 +169,12 @@ final class LoopTimeoutTimerDriver : EventDriverTimers { if (!--tm.refCount) { if (tm.pending) stop(tm.id); m_timers.remove(descriptor); - () @trusted { scope (failure) assert(false); ms_allocator.dispose(tm); } (); + () @trusted { + scope (failure) assert(false); + ms_allocator.dispose(tm); + GC.removeRange(tm); + } (); + return false; }