From 120fd7d833fb03f16ee156d936e446ba25c0e69e Mon Sep 17 00:00:00 2001 From: Michael van der Werve Date: Thu, 23 Feb 2017 16:11:55 +0100 Subject: [PATCH] Fix segfault when assigning 'null' value to the context. --- platform.cpp | 22 +++++++++++++++++----- platform.h | 5 +++++ value.cpp | 2 +- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/platform.cpp b/platform.cpp index b840f2c..ea9f89d 100644 --- a/platform.cpp +++ b/platform.cpp @@ -54,8 +54,17 @@ Platform::Platform() : */ Platform::~Platform() { - // we are no longer running - _running = false; + // we stop the platform first + stop(); +} + +/** + * Stop running the platform. + */ +void Platform::stop() +{ + // we set it to false, but if the old value was not true then we leap out + if (!_running.exchange(false)) return; // signal the thread in case it is waiting for input _condition.notify_one(); @@ -159,8 +168,8 @@ void Platform::shutdown() // still not set, we need to create it now if (result != nullptr) { - // create the platform - delete result; + // we need to stop the platform before disposing and shutting it down + result->stop(); // and shut down the engine (this also takes care of the ICU) and the platform v8::V8::Dispose(); @@ -171,7 +180,10 @@ void Platform::shutdown() // store the new platform platform.store(nullptr, std::memory_order_relaxed); - } + + // delete the platform + delete result; + } } } diff --git a/platform.h b/platform.h index 503c0aa..146ec7d 100644 --- a/platform.h +++ b/platform.h @@ -63,6 +63,11 @@ class Platform : public v8::Platform * @var std::thread */ std::thread _worker; + + /** + * (blocking) method to stop running a platform. + */ + void stop(); /** * Execute some work, this is called diff --git a/value.cpp b/value.cpp index e4a5649..8170fb3 100644 --- a/value.cpp +++ b/value.cpp @@ -83,7 +83,7 @@ v8::Handle value(const Php::Value &input) // the value can be of many types switch (input.type()) { - case Php::Type::Null: /* don't set anything, let it be empty */ break; + case Php::Type::Null: result = v8::Null(Isolate::get()); break; case Php::Type::Numeric: result = v8::Integer::New(Isolate::get(), input); break; case Php::Type::Float: result = v8::Number::New(Isolate::get(), input); break; case Php::Type::Bool: result = v8::Boolean::New(Isolate::get(), input); break;