Skip to content

Commit

Permalink
fix: Cache shared_ptr leak
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni committed May 24, 2023
1 parent 7e24de8 commit 8236cf3
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions NativeScript/runtime/ClassBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@
cache->CtorFuncs.emplace(extendedClassName, poExtendedClassCtorFunc);

IMP newInitialize = imp_implementationWithBlock(^(id self) {
if (!Runtime::IsAlive(isolate) || !isolateWrapper.IsValid()) {
return;
}
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Expand Down Expand Up @@ -261,9 +264,13 @@
/// in order to make both of them destroyable/GC-able. When the JavaScript object is GC-ed we release the native counterpart as well.
void (*retain)(id, SEL) = (void (*)(id, SEL))FindNotOverridenMethod(extendedClass, @selector(retain));
IMP newRetain = imp_implementationWithBlock(^(id self) {
if (!Runtime::IsAlive(isolate) || !isolateWrapper.IsValid()) {
return retain(self, @selector(retain));
}
if ([self retainCount] == 1) {
auto it = cache->Instances.find(self);
if (it != cache->Instances.end()) {
auto innerCache = isolateWrapper.GetCache();
auto it = innerCache->Instances.find(self);
if (it != innerCache->Instances.end()) {
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Expand All @@ -283,12 +290,14 @@
void (*release)(id, SEL) = (void (*)(id, SEL))FindNotOverridenMethod(extendedClass, @selector(release));
IMP newRelease = imp_implementationWithBlock(^(id self) {
if (!Runtime::IsAlive(isolate) || !isolateWrapper.IsValid()) {
release(self, @selector(release));
return;
}

if ([self retainCount] == 2) {
auto it = cache->Instances.find(self);
if (it != cache->Instances.end()) {
auto innerCache = isolateWrapper.GetCache();
auto it = innerCache->Instances.find(self);
if (it != innerCache->Instances.end()) {
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Expand Down

0 comments on commit 8236cf3

Please sign in to comment.