Skip to content

Commit 1885fe5

Browse files
committed
LibWeb: Add GC finalizer to Layout::ImageBox
It's not safe to unregister ImageBox from the browsing context in the destructor (since the browsing context may have already been swept and destroyed).
1 parent c877eb4 commit 1885fe5

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Userland/Libraries/LibWeb/Layout/ImageBox.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ ImageBox::ImageBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr
1818
browsing_context().register_viewport_client(*this);
1919
}
2020

21-
ImageBox::~ImageBox()
21+
ImageBox::~ImageBox() = default;
22+
23+
void ImageBox::finalize()
2224
{
25+
Base::finalize();
26+
27+
// NOTE: We unregister from the browsing context in finalize() to avoid trouble
28+
// in the scenario where our BrowsingContext has already been swept by GC.
2329
browsing_context().unregister_viewport_client(*this);
2430
}
2531

Userland/Libraries/LibWeb/Layout/ImageBox.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Web::Layout {
1414

15-
class ImageBox
15+
class ImageBox final
1616
: public ReplacedBox
1717
, public HTML::BrowsingContext::ViewportClient {
1818
JS_CELL(ImageBox, ReplacedBox);
@@ -37,6 +37,9 @@ class ImageBox
3737
// ^BrowsingContext::ViewportClient
3838
virtual void browsing_context_did_set_viewport_rect(Gfx::IntRect const&) final;
3939

40+
// ^JS::Cell
41+
virtual void finalize() override;
42+
4043
int preferred_width() const;
4144
int preferred_height() const;
4245

0 commit comments

Comments
 (0)