mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-15 19:13:07 +01:00
Fix segfault on exit
By ensuring duk_context is disposed last in ScriptEngine
This commit is contained in:
@@ -31,6 +31,20 @@ using namespace OpenRCT2::Scripting;
|
||||
|
||||
static std::string Stringify(duk_context * ctx, duk_idx_t idx);
|
||||
|
||||
DukContext::DukContext()
|
||||
{
|
||||
_context = duk_create_heap_default();
|
||||
if (_context == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Unable to initialise duktape context.");
|
||||
}
|
||||
}
|
||||
|
||||
DukContext::~DukContext()
|
||||
{
|
||||
duk_destroy_heap(_context);
|
||||
}
|
||||
|
||||
ScriptEngine::ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env) :
|
||||
_console(console),
|
||||
_env(env),
|
||||
@@ -38,20 +52,9 @@ ScriptEngine::ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& en
|
||||
{
|
||||
}
|
||||
|
||||
ScriptEngine::~ScriptEngine()
|
||||
{
|
||||
duk_destroy_heap(_context);
|
||||
}
|
||||
|
||||
void ScriptEngine::Initialise()
|
||||
{
|
||||
_context = duk_create_heap_default();
|
||||
if (_context == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Unable to initialise duktape context.");
|
||||
}
|
||||
|
||||
auto ctx = _context;
|
||||
auto ctx = (duk_context*)_context;
|
||||
ScConsole::Register(ctx);
|
||||
ScContext::Register(ctx);
|
||||
ScDisposable::Register(ctx);
|
||||
|
||||
@@ -58,13 +58,30 @@ namespace OpenRCT2::Scripting
|
||||
std::shared_ptr<Plugin> GetCurrentPlugin() { return _plugin; }
|
||||
};
|
||||
|
||||
class DukContext
|
||||
{
|
||||
private:
|
||||
duk_context * _context{};
|
||||
|
||||
public:
|
||||
DukContext();
|
||||
DukContext(DukContext&) = delete;
|
||||
DukContext(DukContext&& src)
|
||||
: _context(std::move(src._context))
|
||||
{
|
||||
}
|
||||
~DukContext();
|
||||
|
||||
operator duk_context*() { return _context; }
|
||||
};
|
||||
|
||||
class ScriptEngine
|
||||
{
|
||||
private:
|
||||
InteractiveConsole& _console;
|
||||
IPlatformEnvironment& _env;
|
||||
DukContext _context;
|
||||
bool _initialised{};
|
||||
duk_context * _context{};
|
||||
std::queue<std::tuple<std::promise<void>, std::string>> _evalQueue;
|
||||
std::vector<std::shared_ptr<Plugin>> _plugins;
|
||||
uint32 _lastHotReloadCheckTick{};
|
||||
@@ -74,7 +91,6 @@ namespace OpenRCT2::Scripting
|
||||
public:
|
||||
ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env);
|
||||
ScriptEngine(ScriptEngine&) = delete;
|
||||
~ScriptEngine();
|
||||
|
||||
HookEngine& GetHookEngine() { return _hookEngine; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user