From 72ac2125bc8dd28bfd8d94e555458664f9995111 Mon Sep 17 00:00:00 2001 From: Peter Argany Date: Wed, 25 Mar 2020 12:21:42 -0700 Subject: [PATCH] Fix retaining self in block in LogBox impl Summary: Logbox has a retain cycle (see linked task for my deeper investigation). This diff doesn't fix the retain cycle, but it's just good practice to not retain self strongly in blocks. Changelog: [iOS][Internal] Fix retaining self in block in LogBox implementation Reviewed By: shergin Differential Revision: D20630693 fbshipit-source-id: cf399495e9bcd1917932fcc0e9c9d2d2a32bf6f0 --- React/CoreModules/RCTLogBox.mm | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/React/CoreModules/RCTLogBox.mm b/React/CoreModules/RCTLogBox.mm index 5e01f9ad017..0a3dc677bea 100644 --- a/React/CoreModules/RCTLogBox.mm +++ b/React/CoreModules/RCTLogBox.mm @@ -91,11 +91,16 @@ RCT_EXPORT_MODULE() RCT_EXPORT_METHOD(show) { if (RCTRedBoxGetEnabled()) { + __weak RCTLogBox *weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - if (!self->_view) { - self->_view = [[RCTLogBoxView alloc] initWithFrame:[UIScreen mainScreen].bounds bridge:self->_bridge]; + __strong RCTLogBox *strongSelf = weakSelf; + if (!strongSelf) { + return; } - [self->_view show]; + if (!strongSelf->_view) { + strongSelf->_view = [[RCTLogBoxView alloc] initWithFrame:[UIScreen mainScreen].bounds bridge:self->_bridge]; + } + [strongSelf->_view show]; }); } } @@ -103,8 +108,13 @@ RCT_EXPORT_METHOD(show) RCT_EXPORT_METHOD(hide) { if (RCTRedBoxGetEnabled()) { + __weak RCTLogBox *weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - self->_view = nil; + __strong RCTLogBox *strongSelf = weakSelf; + if (!strongSelf) { + return; + } + strongSelf->_view = nil; }); } }