Skip to content

Commit f8be783

Browse files
tadeuzagallofacebook-github-bot-6
authored andcommitted
Document intentional retain cycle on RCTJavaScriptContext
Summary: public More people wanted to understand the motivation behind the intentional retain cycle in `RCTJavaScriptContext`, add a small comment with some context. Reviewed By: jspahrsummers Differential Revision: D2738930 fb-gh-sync-id: d8c950778eb6bf3eaca627aabb6c98335d25d1fc
1 parent 0ce2bce commit f8be783

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

React/Executors/RCTContextExecutor.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,22 @@ - (instancetype)initWithJSContext:(JSContext *)context NS_DESIGNATED_INITIALIZER
5151

5252
@implementation RCTJavaScriptContext
5353
{
54-
RCTJavaScriptContext *_self;
54+
RCTJavaScriptContext *_selfReference;
5555
}
5656

5757
- (instancetype)initWithJSContext:(JSContext *)context
5858
{
5959
if ((self = [super init])) {
6060
_context = context;
61-
_self = self;
61+
62+
/**
63+
* Explicitly introduce a retain cycle here - The RCTContextExecutor might
64+
* be deallocated while there's still work enqueued in the JS thread, so
65+
* we wouldn't be able kill the JSContext. Instead we create this retain
66+
* cycle, and enqueue the -invalidate message in this object, it then
67+
* releases the JSContext, breaks the cycle and stops the runloop.
68+
*/
69+
_selfReference = self;
6270
}
6371
return self;
6472
}
@@ -79,7 +87,7 @@ - (void)invalidate
7987
{
8088
if (self.isValid) {
8189
_context = nil;
82-
_self = nil;
90+
_selfReference = nil;
8391
}
8492
}
8593

0 commit comments

Comments
 (0)