From cbf2881bf7159b5578dfe7ed7e7cbc950188eb29 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Tue, 25 Feb 2020 12:05:25 +0300 Subject: [PATCH] LibThread: Fix destroying background actions In the old model, before bc319d9e8873734bb8e8cea3d762d7fab2ded887, the parent (the background thread) would delete us when it exits (i.e. never), so we had to keep track of our own refcount in order to destroy ourselves when we're done. With bc319d9e8873734bb8e8cea3d762d7fab2ded887, the parent keeps additional reference to us, so: * There should be no need to explicitly ref() ourselves * The unref() would not get rid of the last reference to us anymore The latter is why all the BackgroundAction's were getting leaked. Fix this by simply unparenting ourselves from the background thread when we're done. --- Libraries/LibThread/BackgroundAction.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Libraries/LibThread/BackgroundAction.h b/Libraries/LibThread/BackgroundAction.h index 9bc7224956..b7b5b0810c 100644 --- a/Libraries/LibThread/BackgroundAction.h +++ b/Libraries/LibThread/BackgroundAction.h @@ -75,17 +75,17 @@ private: { LOCKER(all_actions().lock()); - this->ref(); all_actions().resource().enqueue([this] { m_result = m_action(); if (m_on_complete) { Core::EventLoop::current().post_event(*this, make([this](auto&) { m_on_complete(m_result.release_value()); - this->unref(); + this->remove_from_parent(); })); Core::EventLoop::wake(); - } else - this->unref(); + } else { + this->remove_from_parent(); + } }); }