summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI
diff options
context:
space:
mode:
authorTom <tomut@yahoo.com>2020-12-29 13:14:21 -0700
committerAndreas Kling <kling@serenityos.org>2020-12-31 00:39:43 +0100
commit54eeb8ee9a57850aca5056a5834bfb86a210bf1a (patch)
tree62bc71286635f0258b86aac7d2f839c2a88d56ed /Libraries/LibGUI
parent3e00e3da7246f588a9839105eaaf680aaccba21d (diff)
downloadserenity-54eeb8ee9a57850aca5056a5834bfb86a210bf1a.zip
AK: Fix a race condition with WeakPtr<T>::strong_ref and destruction
Since RefPtr<T> decrements the ref counter to 0 and after that starts destructing the object, there is a window where the ref count is 0 and the weak references have not been revoked. Also change WeakLink to be able to obtain a strong reference concurrently and block revoking instead, which should happen a lot less often. Fixes a problem observed in #4621
Diffstat (limited to 'Libraries/LibGUI')
-rw-r--r--Libraries/LibGUI/Application.cpp5
1 files changed, 5 insertions, 0 deletions
diff --git a/Libraries/LibGUI/Application.cpp b/Libraries/LibGUI/Application.cpp
index a70e7d5b39..55cc9373c9 100644
--- a/Libraries/LibGUI/Application.cpp
+++ b/Libraries/LibGUI/Application.cpp
@@ -72,6 +72,11 @@ static NeverDestroyed<WeakPtr<Application>> s_the;
Application* Application::the()
{
+ // NOTE: If we don't explicitly call revoke_weak_ptrs() in the
+ // ~Application destructor, we would have to change this to
+ // return s_the->strong_ref().ptr();
+ // This is because this is using the unsafe operator*/operator->
+ // that do not have the ability to check the ref count!
return *s_the;
}