From 3df9861814219915a4c4643ea57d4db7f9af2776 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 18 Sep 2022 02:15:01 +0200 Subject: [PATCH] LibWeb: Capture self as a WeakPtr in ResourceClient::set_resource() It's not safe to capture `this` as a raw pointer here, since nothing is guaranteed to keep the ResourceClient alive (even if the Resource stays alive.) --- Userland/Libraries/LibWeb/Loader/Resource.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWeb/Loader/Resource.cpp b/Userland/Libraries/LibWeb/Loader/Resource.cpp index 79c54edcfa..eaf79fa751 100644 --- a/Userland/Libraries/LibWeb/Loader/Resource.cpp +++ b/Userland/Libraries/LibWeb/Loader/Resource.cpp @@ -168,17 +168,20 @@ void ResourceClient::set_resource(Resource* resource) // This ensures that these callbacks always happen in a consistent way, instead of being invoked // synchronously in some cases, and asynchronously in others. if (resource->is_loaded() || resource->is_failed()) { - Platform::EventLoopPlugin::the().deferred_invoke([this, strong_resource = NonnullRefPtr { *m_resource }] { - if (m_resource != strong_resource.ptr()) + Platform::EventLoopPlugin::the().deferred_invoke([weak_this = make_weak_ptr(), strong_resource = NonnullRefPtr { *m_resource }]() mutable { + if (!weak_this) + return; + + if (weak_this->m_resource != strong_resource.ptr()) return; // Make sure that reused resources also have their load callback fired. - if (m_resource->is_loaded()) - resource_did_load(); + if (weak_this->m_resource->is_loaded()) + weak_this->resource_did_load(); // Make sure that reused resources also have their fail callback fired. - if (m_resource->is_failed()) - resource_did_fail(); + if (weak_this->m_resource->is_failed()) + weak_this->resource_did_fail(); }); } }