Skip to content

Commit 03895a0

Browse files
committed
fix: safeguards on dictionary adapter dealloc
1 parent 66ba05c commit 03895a0

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

NativeScript/runtime/DictionaryAdapter.mm

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,15 @@ - (void)dealloc {
158158
@implementation DictionaryAdapter {
159159
IsolateWrapper* wrapper_;
160160
std::shared_ptr<Persistent<Value>> object_;
161+
ObjCDataWrapper* dataWrapper_;
161162
}
162163

163164
- (instancetype)initWithJSObject:(Local<Object>)jsObject isolate:(Isolate*)isolate {
164165
if (self) {
165166
self->wrapper_ = new IsolateWrapper(isolate);
166167
self->object_ = std::make_shared<Persistent<Value>>(isolate, jsObject);
167168
self->wrapper_->GetCache()->Instances.emplace(self, self->object_);
168-
tns::SetValue(isolate, jsObject, new ObjCDataWrapper(self));
169+
tns::SetValue(isolate, jsObject, (self->dataWrapper_ = new ObjCDataWrapper(self)));
169170
}
170171

171172
return self;
@@ -253,17 +254,25 @@ - (NSEnumerator*)keyEnumerator {
253254
}
254255

255256
- (void)dealloc {
256-
if(wrapper_->IsValid()) {
257+
if (wrapper_->IsValid()) {
257258
Isolate* isolate = wrapper_->Isolate();
259+
v8::Locker locker(isolate);
260+
Isolate::Scope isolate_scope(isolate);
261+
HandleScope handle_scope(isolate);
258262
wrapper_->GetCache()->Instances.erase(self);
259263
Local<Value> value = self->object_->Get(isolate);
260264
BaseDataWrapper* wrapper = tns::GetValue(isolate, value);
261265
if (wrapper != nullptr) {
266+
if (wrapper == dataWrapper_) {
267+
dataWrapper_ = nullptr;
268+
}
262269
tns::DeleteValue(isolate, value);
263270
delete wrapper;
264271
}
265272
}
266-
self->object_ = nil;
273+
if (dataWrapper_ != nullptr) {
274+
delete dataWrapper_;
275+
}
267276
self->object_ = nullptr;
268277
delete self->wrapper_;
269278

0 commit comments

Comments
 (0)