⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 243239 in webkit


Ignore:
Timestamp:
Mar 20, 2019, 2:23:02 PM (7 years ago)
Author:
rniwa@webkit.org
Message:

[CSS OM] StyledElementInlineStylePropertyMap creates a Ref cycle with its owner element
https://bugs.webkit.org/show_bug.cgi?id=195987

Reviewed by Simon Fraser.

Source/WebCore:

StyledElementInlineStylePropertyMap was leaking every element for which it was created because due to
a reference cycle. The StyledElementInlineStylePropertyMap holds onto its element using Ref and
the element also stores StyledElementInlineStylePropertyMap in ElementRareData using RefPtr.

Fixed the cycle by making the reference from StyledElementInlineStylePropertyMap weak. For now we use
a raw pointer because we can't create a WeakPtr of an element yet.

Test: css-typedom/attribute-style-map-should-not-leak-every-element.html

  • css/typedom/StylePropertyMap.h:

(WebCore::StylePropertyMap): Added clearElement as a virtual function.

  • dom/Element.cpp:

(WebCore::Element::~Element): Clear the element pointer in StyledElementInlineStylePropertyMap.

  • dom/StyledElement.cpp:

(WebCore::StyledElementInlineStylePropertyMap::get): Added a null check for m_element.
(WebCore::StyledElementInlineStylePropertyMap::StyledElementInlineStylePropertyMap):
(WebCore::StyledElementInlineStylePropertyMap::clearElement): Added.
(WebCore::StyledElementInlineStylePropertyMap): Use a raw pointer instead of Ref to StyledElement
to avoid the leak.

  • platform/graphics/CustomPaintImage.cpp:

(WebCore::HashMapStylePropertyMap::clearElement): Added.

LayoutTests:

Added a regression test.

  • css-typedom/attribute-style-map-should-not-leak-every-element-expected.txt: Added.
  • css-typedom/attribute-style-map-should-not-leak-every-element.html: Added.
Location:
trunk
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r243235 r243239  
     12019-03-19  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        [CSS OM] StyledElementInlineStylePropertyMap creates a Ref cycle with its owner element
     4        https://bugs.webkit.org/show_bug.cgi?id=195987
     5
     6        Reviewed by Simon Fraser.
     7
     8        Added a regression test.
     9
     10        * css-typedom/attribute-style-map-should-not-leak-every-element-expected.txt: Added.
     11        * css-typedom/attribute-style-map-should-not-leak-every-element.html: Added.
     12
    1132019-03-20  Antoine Quint  <graouts@apple.com>
    214
  • trunk/Source/WebCore/ChangeLog

    r243233 r243239  
     12019-03-19  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        [CSS OM] StyledElementInlineStylePropertyMap creates a Ref cycle with its owner element
     4        https://bugs.webkit.org/show_bug.cgi?id=195987
     5
     6        Reviewed by Simon Fraser.
     7
     8        StyledElementInlineStylePropertyMap was leaking every element for which it was created because due to
     9        a reference cycle. The StyledElementInlineStylePropertyMap holds onto its element using Ref and
     10        the element also stores StyledElementInlineStylePropertyMap in ElementRareData using RefPtr.
     11
     12        Fixed the cycle by making the reference from StyledElementInlineStylePropertyMap weak. For now we use
     13        a raw pointer because we can't create a WeakPtr of an element yet.
     14
     15        Test: css-typedom/attribute-style-map-should-not-leak-every-element.html
     16
     17        * css/typedom/StylePropertyMap.h:
     18        (WebCore::StylePropertyMap): Added clearElement as a virtual function.
     19        * dom/Element.cpp:
     20        (WebCore::Element::~Element): Clear the element pointer in StyledElementInlineStylePropertyMap.
     21        * dom/StyledElement.cpp:
     22        (WebCore::StyledElementInlineStylePropertyMap::get): Added a null check for m_element.
     23        (WebCore::StyledElementInlineStylePropertyMap::StyledElementInlineStylePropertyMap):
     24        (WebCore::StyledElementInlineStylePropertyMap::clearElement): Added.
     25        (WebCore::StyledElementInlineStylePropertyMap): Use a raw pointer instead of Ref to StyledElement
     26        to avoid the leak.
     27        * platform/graphics/CustomPaintImage.cpp:
     28        (WebCore::HashMapStylePropertyMap::clearElement): Added.
     29
    1302019-03-19  Ryosuke Niwa  <rniwa@webkit.org>
    231
  • trunk/Source/WebCore/css/typedom/StylePropertyMap.h

    r239341 r243239  
    3636
    3737class StylePropertyMap : public StylePropertyMapReadOnly {
     38public:
     39    virtual void clearElement() = 0;
    3840};
    3941
  • trunk/Source/WebCore/dom/Element.cpp

    r243163 r243239  
    204204        detachAllAttrNodesFromElement();
    205205
     206#if ENABLE(CSS_TYPED_OM)
     207    if (hasRareData()) {
     208        if (auto* map = elementRareData()->attributeStyleMap())
     209            map->clearElement();
     210    }
     211#endif
     212
    206213    if (hasPendingResources()) {
    207214        document().accessSVGExtensions().removeElementFromPendingResources(*this);
  • trunk/Source/WebCore/dom/StyledElement.cpp

    r239341 r243239  
    8989    RefPtr<TypedOMCSSStyleValue> get(const String& property) const final
    9090    {
    91         return extractInlineProperty(property, m_element.get());
     91        ASSERT(m_element); // Hitting this assertion would imply a GC bug. Element is collected while this property map is alive.
     92        if (!m_element)
     93            return nullptr;
     94        return extractInlineProperty(property, *m_element);
    9295    }
    9396
    9497    explicit StyledElementInlineStylePropertyMap(StyledElement& element)
    95         : m_element(makeRef(element))
     98        : m_element(&element)
    9699    {
    97100    }
     101
     102    void clearElement() override { m_element = nullptr; }
    98103
    99104    static RefPtr<TypedOMCSSStyleValue> extractInlineProperty(const String& name, StyledElement& element)
     
    115120    }
    116121
    117     Ref<StyledElement> m_element;
     122    StyledElement* m_element { nullptr };
    118123};
    119124
  • trunk/Source/WebCore/platform/graphics/CustomPaintImage.cpp

    r239341 r243239  
    106106    }
    107107
     108    void clearElement() override { }
     109
    108110    RefPtr<TypedOMCSSStyleValue> get(const String& property) const final { return makeRefPtr(m_map.get(property)); }
    109111
Note: See TracChangeset for help on using the changeset viewer.