Skip to content

Commit 8236cf3

Browse files
committed
fix: Cache shared_ptr leak
1 parent 7e24de8 commit 8236cf3

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

NativeScript/runtime/ClassBuilder.mm

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@
227227
cache->CtorFuncs.emplace(extendedClassName, poExtendedClassCtorFunc);
228228

229229
IMP newInitialize = imp_implementationWithBlock(^(id self) {
230+
if (!Runtime::IsAlive(isolate) || !isolateWrapper.IsValid()) {
231+
return;
232+
}
230233
v8::Locker locker(isolate);
231234
Isolate::Scope isolate_scope(isolate);
232235
HandleScope handle_scope(isolate);
@@ -261,9 +264,13 @@
261264
/// in order to make both of them destroyable/GC-able. When the JavaScript object is GC-ed we release the native counterpart as well.
262265
void (*retain)(id, SEL) = (void (*)(id, SEL))FindNotOverridenMethod(extendedClass, @selector(retain));
263266
IMP newRetain = imp_implementationWithBlock(^(id self) {
267+
if (!Runtime::IsAlive(isolate) || !isolateWrapper.IsValid()) {
268+
return retain(self, @selector(retain));
269+
}
264270
if ([self retainCount] == 1) {
265-
auto it = cache->Instances.find(self);
266-
if (it != cache->Instances.end()) {
271+
auto innerCache = isolateWrapper.GetCache();
272+
auto it = innerCache->Instances.find(self);
273+
if (it != innerCache->Instances.end()) {
267274
v8::Locker locker(isolate);
268275
Isolate::Scope isolate_scope(isolate);
269276
HandleScope handle_scope(isolate);
@@ -283,12 +290,14 @@
283290
void (*release)(id, SEL) = (void (*)(id, SEL))FindNotOverridenMethod(extendedClass, @selector(release));
284291
IMP newRelease = imp_implementationWithBlock(^(id self) {
285292
if (!Runtime::IsAlive(isolate) || !isolateWrapper.IsValid()) {
293+
release(self, @selector(release));
286294
return;
287295
}
288296

289297
if ([self retainCount] == 2) {
290-
auto it = cache->Instances.find(self);
291-
if (it != cache->Instances.end()) {
298+
auto innerCache = isolateWrapper.GetCache();
299+
auto it = innerCache->Instances.find(self);
300+
if (it != innerCache->Instances.end()) {
292301
v8::Locker locker(isolate);
293302
Isolate::Scope isolate_scope(isolate);
294303
HandleScope handle_scope(isolate);

0 commit comments

Comments
 (0)