mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
490e33dd88
Summary: Clearing `props` and `state` (which we don't use) allows avoiding retain cycles. The memory leak was caused by a retain cycle: AttributedString -> Fragment -> ShadowView -> PharagraphState -> TextLayoutManager -> Cache -> AttributedString. We don't use `props` and `state` parts of `ShadowView` inside `AttributedString`. Changelog: [Internal] Fabric-specific internal change. Reviewed By: mdvacca Differential Revision: D20182791 fbshipit-source-id: 2ddc5d53a1cb594e3d1cc39933e8958eb6425389
78 lines
2.6 KiB
C++
78 lines
2.6 KiB
C++
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "BaseTextShadowNode.h"
|
|
|
|
#include <react/components/text/RawTextProps.h>
|
|
#include <react/components/text/RawTextShadowNode.h>
|
|
#include <react/components/text/TextProps.h>
|
|
#include <react/components/text/TextShadowNode.h>
|
|
#include <react/debug/DebugStringConvertibleItem.h>
|
|
#include <react/mounting/ShadowView.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
inline ShadowView shadowViewFromShadowNode(ShadowNode const &shadowNode) {
|
|
auto shadowView = ShadowView{shadowNode};
|
|
// Clearing `props` and `state` (which we don't use) allows avoiding retain
|
|
// cycles.
|
|
shadowView.props = nullptr;
|
|
shadowView.state = nullptr;
|
|
return shadowView;
|
|
}
|
|
|
|
AttributedString BaseTextShadowNode::getAttributedString(
|
|
TextAttributes const &textAttributes,
|
|
ShadowNode const &parentNode) {
|
|
auto attributedString = AttributedString{};
|
|
|
|
for (auto const &childNode : parentNode.getChildren()) {
|
|
// RawShadowNode
|
|
auto rawTextShadowNode =
|
|
std::dynamic_pointer_cast<const RawTextShadowNode>(childNode);
|
|
if (rawTextShadowNode) {
|
|
auto fragment = AttributedString::Fragment{};
|
|
fragment.string = rawTextShadowNode->getConcreteProps().text;
|
|
fragment.textAttributes = textAttributes;
|
|
|
|
// Storing a retaining pointer to `ParagraphShadowNode` inside
|
|
// `attributedString` causes a retain cycle (besides that fact that we
|
|
// don't need it at all). Storing a `ShadowView` instance instead of
|
|
// `ShadowNode` should properly fix this problem.
|
|
fragment.parentShadowView = shadowViewFromShadowNode(parentNode);
|
|
attributedString.appendFragment(fragment);
|
|
continue;
|
|
}
|
|
|
|
// TextShadowNode
|
|
auto textShadowNode =
|
|
std::dynamic_pointer_cast<const TextShadowNode>(childNode);
|
|
if (textShadowNode) {
|
|
auto localTextAttributes = textAttributes;
|
|
localTextAttributes.apply(
|
|
textShadowNode->getConcreteProps().textAttributes);
|
|
attributedString.appendAttributedString(
|
|
textShadowNode->getAttributedString(
|
|
localTextAttributes, *textShadowNode));
|
|
continue;
|
|
}
|
|
|
|
// Any other kind of ShadowNode
|
|
auto fragment = AttributedString::Fragment{};
|
|
fragment.string = AttributedString::Fragment::AttachmentCharacter();
|
|
fragment.parentShadowView = shadowViewFromShadowNode(*childNode);
|
|
fragment.textAttributes = textAttributes;
|
|
attributedString.appendFragment(fragment);
|
|
}
|
|
|
|
return attributedString;
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|