From 8e490d4d87feb31be581fd29bff0bc667c0abd57 Mon Sep 17 00:00:00 2001 From: ericlewis Date: Tue, 12 Mar 2019 19:57:31 -0700 Subject: [PATCH] Fabric: fix border memory leaks (#23815) Summary: This fixes a few memory leaks in fabrics handling of colors for borders, when using CGColorRef's we must be diligent about releasing the memory back. [iOS] [Fixed] - Border style memory leaks Pull Request resolved: https://github.com/facebook/react-native/pull/23815 Differential Revision: D14431250 Pulled By: shergin fbshipit-source-id: dc663c633ae24809cb4841800d31a6ac6eeb8aa5 --- .../ComponentViews/View/RCTViewComponentView.mm | 14 +++++++++----- React/Fabric/RCTConversions.h | 8 +++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm b/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm index 9dd1a72aa..fa0a7d2b0 100644 --- a/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -326,10 +326,12 @@ static RCTCornerRadii RCTCornerRadiiFromBorderRadii(BorderRadii borderRadii) static RCTBorderColors RCTBorderColorsFromBorderColors(BorderColors borderColors) { - return RCTBorderColors{.left = RCTCGColorRefFromSharedColor(borderColors.left), - .top = RCTCGColorRefFromSharedColor(borderColors.top), - .bottom = RCTCGColorRefFromSharedColor(borderColors.bottom), - .right = RCTCGColorRefFromSharedColor(borderColors.right)}; + return RCTBorderColors{ + .left = RCTCGColorRefUnretainedFromSharedColor(borderColors.left), + .top = RCTCGColorRefUnretainedFromSharedColor(borderColors.top), + .bottom = RCTCGColorRefUnretainedFromSharedColor(borderColors.bottom), + .right = RCTCGColorRefUnretainedFromSharedColor(borderColors.right) + }; } static UIEdgeInsets UIEdgeInsetsFromBorderInsets(EdgeInsets edgeInsets) @@ -396,7 +398,9 @@ static RCTBorderStyle RCTBorderStyleFromBorderStyle(BorderStyle borderStyle) } layer.borderWidth = (CGFloat)borderMetrics.borderWidths.left; - layer.borderColor = RCTCGColorRefFromSharedColor(borderMetrics.borderColors.left); + CGColorRef borderColor = RCTCGColorRefFromSharedColor(borderMetrics.borderColors.left); + layer.borderColor = borderColor; + CGColorRelease(borderColor); layer.cornerRadius = (CGFloat)borderMetrics.borderRadii.topLeft; layer.backgroundColor = _backgroundColor.CGColor; _contentView.layer.cornerRadius = (CGFloat)borderMetrics.borderRadii.topLeft; diff --git a/React/Fabric/RCTConversions.h b/React/Fabric/RCTConversions.h index 16ee3ed2d..c4d8f407b 100644 --- a/React/Fabric/RCTConversions.h +++ b/React/Fabric/RCTConversions.h @@ -30,7 +30,13 @@ inline UIColor *_Nullable RCTUIColorFromSharedColor(const facebook::react::Share return sharedColor ? [UIColor colorWithCGColor:sharedColor.get()] : nil; } -inline CGColorRef RCTCGColorRefFromSharedColor(const facebook::react::SharedColor &sharedColor) { + +inline CF_RETURNS_NOT_RETAINED CGColorRef RCTCGColorRefUnretainedFromSharedColor(const facebook::react::SharedColor &sharedColor) { + return sharedColor ? sharedColor.get() : nil; +} + + +inline CF_RETURNS_RETAINED CGColorRef RCTCGColorRefFromSharedColor(const facebook::react::SharedColor &sharedColor) { return sharedColor ? CGColorCreateCopy(sharedColor.get()) : nil; }