Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: memory leak on ArrayAdapter, DictionaryAdapter and NSDataAdapter #170

Merged
merged 1 commit into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 3 additions & 30 deletions NativeScript/runtime/ArrayAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,6 @@ - (id)objectAtIndex:(NSUInteger)index {
return value;
}

- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained _Nullable [_Nonnull])buffer count:(NSUInteger)len {
if (state->state == 0) { // uninitialized
state->state = 1;
void* selfPtr = (__bridge void*)self;
state->mutationsPtr = (unsigned long*)selfPtr;
state->extra[0] = 0; // current index
NSUInteger cnt = [self count];
state->extra[1] = cnt;
}

NSUInteger currentIndex = state->extra[0];
unsigned long length = state->extra[1];
NSUInteger count = 0;
state->itemsPtr = buffer;

@autoreleasepool {
while (count < len && currentIndex < length) {
id obj = [self objectAtIndex:currentIndex];
CFBridgingRetain(obj);
*buffer++ = obj;
currentIndex++;
count++;
}
}

state->extra[0] = currentIndex;

return count;
}

- (void)dealloc {
self->cache_->Instances.erase(self);
Local<Value> value = self->object_->Get(self->isolate_);
Expand All @@ -106,6 +76,9 @@ - (void)dealloc {
delete wrapper;
}
self->object_->Reset();
self->isolate_ = nullptr;
self->cache_ = nullptr;
self->object_ = nullptr;
[super dealloc];
}

Expand Down
6 changes: 4 additions & 2 deletions NativeScript/runtime/DictionaryAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ - (NSEnumerator*)keyEnumerator {
Local<Value> obj = self->object_->Get(self->isolate_);

if (obj->IsMap()) {
self->enumerator_ = [[DictionaryAdapterMapKeysEnumerator alloc] initWithMap:self->object_ isolate:self->isolate_ cache:self->cache_];
self->enumerator_ = [[[DictionaryAdapterMapKeysEnumerator alloc] initWithMap:self->object_ isolate:self->isolate_ cache:self->cache_] autorelease];

return self->enumerator_;
}

self->enumerator_ = [[DictionaryAdapterObjectKeysEnumerator alloc] initWithProperties:self->object_ isolate:self->isolate_ cache:self->cache_];
self->enumerator_ = [[[DictionaryAdapterObjectKeysEnumerator alloc] initWithProperties:self->object_ isolate:self->isolate_ cache:self->cache_] autorelease];

return self->enumerator_;
}
Expand All @@ -261,6 +261,8 @@ - (void)dealloc {
// CFAutorelease(self->enumerator_);
self->enumerator_ = nullptr;
}
self->cache_ = nullptr;
self->object_ = nullptr;

[super dealloc];
}
Expand Down
3 changes: 3 additions & 0 deletions NativeScript/runtime/NSDataAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ - (void)dealloc {
delete wrapper;
}
self->object_->Reset();
self->isolate_ = nullptr;
self->cache_ = nullptr;
self->object_ = nullptr;
[super dealloc];
}

Expand Down