diff --git a/src/openrct2/scripting/ScriptEngine.h b/src/openrct2/scripting/ScriptEngine.h index 8f1c2b6818..cc52747324 100644 --- a/src/openrct2/scripting/ScriptEngine.h +++ b/src/openrct2/scripting/ScriptEngine.h @@ -333,6 +333,8 @@ namespace OpenRCT2::Scripting classId = JS_INVALID_CLASS_ID; if (protoCtx != nullptr) JS_FreeValue(protoCtx, proto); + proto = JS_NULL; + protoCtx = nullptr; } [[nodiscard]] JSValue GetProto() const diff --git a/test/tests/ScriptingTests.cpp b/test/tests/ScriptingTests.cpp index 712a7914fb..f5b0b2527d 100644 --- a/test/tests/ScriptingTests.cpp +++ b/test/tests/ScriptingTests.cpp @@ -73,4 +73,21 @@ TEST_F(ScriptingTests, MultipleSubscribersToSameEventShouldNotCrash) hookEngine.Call(HookType::intervalTick, arg, false); } +TEST(ScriptEngineLifetimeTests, UninitialisedContextCanBeDestroyedAfterInitialisedOne) +{ + gOpenRCT2Headless = true; + gOpenRCT2NoGraphics = true; + + // Registers the global script classes and then frees the JS runtime they belong to. + { + auto context = CreateContext(); + context->GetScriptEngine().Initialise(); + } + + // Unregisters the global script classes again without registering them first. + { + auto context = CreateContext(); + } +} + #endif