1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 17:35:08 +00:00

LibWeb: Break reference cycles so DOM::Document actually gets deleted

When a document reaches ref_count==0, we will now remove all of the
descendant nodes from the document, and also break all the explicit
links (such as the currently hovered element.)

Basically, DOM nodes will keep the document alive even after the
document reaches ref_count==0. This allows JS wrappers to stay alive
and keep the document alive as well. This matches the behavior of
at least some other browsers.

This patch also adds a bunch of sanity checking assertions around
DOM teardown, to help catch mistakes in the future.

Fixes #3771.
This commit is contained in:
Andreas Kling 2020-10-22 23:38:14 +02:00
parent 018b458962
commit f79e28bd65
4 changed files with 79 additions and 14 deletions

View file

@ -54,14 +54,18 @@ Node::Node(Document& document, NodeType type)
, m_document(&document)
, m_type(type)
{
m_document->ref_from_node({});
if (!is_document())
m_document->ref_from_node({});
}
Node::~Node()
{
ASSERT(m_deletion_has_begun);
if (layout_node() && layout_node()->parent())
layout_node()->parent()->remove_child(*layout_node());
m_document->unref_from_node({});
if (!is_document())
m_document->unref_from_node({});
}
const HTML::HTMLAnchorElement* Node::enclosing_link_element() const
@ -222,6 +226,7 @@ void Node::removed_last_ref()
downcast<Document>(*this).removed_last_ref();
return;
}
m_deletion_has_begun = true;
delete this;
}