Skip to content

Truly independent web browser

LibWeb: Let image style value registrations outlive a collected client

Shannon Booth512e88425adf
diff --git a/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp
index a746d37ad5d..2aa8fd837c5 100644
--- a/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp
+++ b/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp
@@ -57,7 +57,8 @@ ImageStyleValueResource::ImageStyleValueResource(GC::Ref<HTML::SharedResourceReq
ImageStyleValueResource::~ImageStyleValueResource()
{
- VERIFY(m_registration_counts_by_image_style_value.is_empty());
+ // Registrations are only left behind by clients that were collected along with our document, which then destroys
+ // us. See ImageStyleValue::unregister_client().
unregister_with_decoded_image_data_if_needed();
}
@@ -257,7 +258,8 @@ void ImageStyleValue::unregister_client(Client& client) const
if (!resource)
return;
- // The document owns the resource, so a collected document has already destroyed it.
+ // The document owns the resource. A client only loses its document when the document is collected, which destroys
+ // the resource along with any registrations that are left.
auto document = client.document();
if (!document)
return;
diff --git a/Libraries/LibWeb/HTML/DecodedImageData.cpp b/Libraries/LibWeb/HTML/DecodedImageData.cpp
index a18933e4fa9..7af7063953c 100644
--- a/Libraries/LibWeb/HTML/DecodedImageData.cpp
+++ b/Libraries/LibWeb/HTML/DecodedImageData.cpp
@@ -5,6 +5,8 @@
*/
#include <LibCompositing/DisplayList/DisplayList.h>
+#include <LibGC/Heap.h>
+#include <LibGC/WeakInlines.h>
#include <LibWeb/HTML/DecodedImageData.h>
namespace Web::HTML {
@@ -17,18 +19,18 @@ void DecodedImageData::Client::register_with_decoded_image_data_if_needed()
return;
image_data->m_clients.set(this);
+ m_registered_image_data = image_data;
image_data->on_client_registered();
}
+// Clients unregister from their finalizers and destructors, when what leads to their current image data may already
+// have been collected. So unregister from the image data we registered with, if it is still alive.
void DecodedImageData::Client::unregister_with_decoded_image_data_if_needed()
{
- auto const& image_data = decoded_image_data();
-
- if (!image_data)
- return;
-
- image_data->m_clients.remove(this);
+ if (auto image_data = m_registered_image_data.ptr())
+ image_data->m_clients.remove(this);
+ m_registered_image_data = nullptr;
}
DecodedImageData::DecodedImageData() = default;
diff --git a/Libraries/LibWeb/HTML/DecodedImageData.h b/Libraries/LibWeb/HTML/DecodedImageData.h
index 83542b21d7e..5ce0eaacdf7 100644
--- a/Libraries/LibWeb/HTML/DecodedImageData.h
+++ b/Libraries/LibWeb/HTML/DecodedImageData.h
@@ -9,6 +9,7 @@
#include <AK/Optional.h>
#include <AK/RefCounted.h>
#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>
+#include <LibGC/Weak.h>
#include <LibGfx/Color.h>
#include <LibGfx/DecodedImageFrame.h>
#include <LibGfx/ScalingMode.h>
@@ -35,6 +36,9 @@ public:
protected:
void register_with_decoded_image_data_if_needed();
void unregister_with_decoded_image_data_if_needed();
+
+ private:
+ GC::Weak<DecodedImageData> m_registered_image_data;
};
virtual ~DecodedImageData();