diff --git a/NativeScript/runtime/Runtime.mm b/NativeScript/runtime/Runtime.mm index 63f61924..3f132be0 100644 --- a/NativeScript/runtime/Runtime.mm +++ b/NativeScript/runtime/Runtime.mm @@ -34,6 +34,22 @@ SimpleAllocator allocator_; NSDictionary* AppPackageJson = nil; +void DisposeIsolateWhenPossible(Isolate* isolate) { + // most of the time, this will never delay disposal + // occasionally this can happen when the runtime is destroyed by actions of its own isolate + // as an example: isolate calls exit(0), which in turn destroys the Runtime unique_ptr + // another scenario is when embedding nativescript, if the embedder deletes the runtime as a result of a callback from JS + // in the case of exit(0), the app will die before actually disposing the isolate, which isn't a problem + if (isolate->IsInUse()) { + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_MSEC)), + dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ + DisposeIsolateWhenPossible(isolate); + }); + } else { + isolate->Dispose(); + } +} + void Runtime::Initialize() { MetaFile::setInstance(RuntimeConfig.MetadataPtr); } @@ -82,7 +98,7 @@ this->isolate_->SetData(Constants::RUNTIME_SLOT, nullptr); } - this->isolate_->Dispose(); + DisposeIsolateWhenPossible(this->isolate_); currentRuntime_ = nullptr; }