Skip to content

Commit

Permalink
fix(ios): release resources after LiveView restart
Browse files Browse the repository at this point in the history
  • Loading branch information
jquick-axway committed Jun 4, 2021
1 parent f0e19b8 commit 9ed66b7
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions iphone/titanium/HyperloopModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ -(JSObjectRef) propsObject;
static JSObjectRef HLObjectMake(JSContextRef ctx, JSClassRef cls, id obj);
JSObjectRef HyperloopGetWrapperForId(id obj);

static void HyperloopRelease () {
if (context) {
[callbacks removeAllObjects];
[modules removeAllObjects];
ARCRelease(callbacks);
ARCRelease(modules);
classClassRef = NULL;
pointerClassRef = NULL;
constructorClassRef = NULL;
objectClassRef = NULL;
bridge = nil;
context = nil;
callbacks = nil;
modules = nil;
}
if (javaScriptWrappers) {
CFRelease(javaScriptWrappers);
javaScriptWrappers = NULL;
}
}

/**
* gets the memory address of an Objective-C object as a string
*/
Expand Down Expand Up @@ -987,6 +1008,10 @@ @implementation Hyperloop
*/
+(void)willStartNewContext:(KrollContext *)kroll bridge:(KrollBridge *)krollbridge {
// NSLog(@"[TRACE][HYPERLOOP] willStartNewContext %@", kroll);

// Release objects belonging to last context. (Will only happen if LiveView restarts app's JS runtime.)
HyperloopRelease();

context = kroll;
bridge = krollbridge;
JSGlobalContextRef ctx = (JSGlobalContextRef)[kroll context];
Expand Down Expand Up @@ -1107,30 +1132,7 @@ +(void)didStartNewContext:(KrollContext *)kroll bridge:(KrollBridge *)bridge{
*/
+(void)willStopNewContext:(KrollContext *)kroll bridge:(KrollBridge *)bridge{
// NSLog(@"[TRACE][HYPERLOOP] willStopNewContext %@", kroll);
if (context) {
[callbacks removeAllObjects];
[modules removeAllObjects];
JSGlobalContextRef ctx = (JSGlobalContextRef)[kroll context];
JSStringRef prop = JSStringCreateWithUTF8CString("Hyperloop");
JSObjectRef globalObjectRef = JSContextGetGlobalObject(ctx);
JSValueRef objectRef = JSObjectGetProperty(ctx, globalObjectRef, prop, NULL);
JSValueUnprotect(ctx, objectRef);
JSObjectDeleteProperty(ctx, globalObjectRef, prop, NULL);
JSStringRelease(prop);
JSClassRelease(classClassRef);
JSClassRelease(pointerClassRef);
JSClassRelease(constructorClassRef);
JSClassRelease(objectClassRef);
JSGlobalContextRelease(ctx);
ARCRelease(callbacks);
ARCRelease(modules);
classClassRef = nil;
pointerClassRef = nil;
constructorClassRef = nil;
context = nil;
callbacks = nil;
modules = nil;
}
HyperloopRelease();
}

/**
Expand Down

0 comments on commit 9ed66b7

Please sign in to comment.