From 85b517cd9318ccc382226c6db82feec035ec99fc Mon Sep 17 00:00:00 2001 From: Charles Pigott Date: Sat, 2 Nov 2019 12:50:42 +0000 Subject: [PATCH] Fix #7606: Game crash when trying to clean up a crashed script Also fix another possible memory leak (cherry picked from commit cdf9ff6f5f505517600dc487bc49d9e3cca90d9a) --- src/script/script_instance.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp index 3468bff446..84dc52c81c 100644 --- a/src/script/script_instance.cpp +++ b/src/script/script_instance.cpp @@ -91,6 +91,10 @@ void ScriptInstance::Initialize(const char *main_script, const char *instance_na /* Create the main-class */ this->instance = MallocT(1); if (!this->engine->CreateClassInstance(instance_name, this->controller, this->instance)) { + /* If CreateClassInstance has returned false instance has not been + * registered with squirrel, so avoid trying to Release it by clearing it now */ + delete this->instance; + this->instance = nullptr; this->Died(); return; } @@ -156,6 +160,7 @@ void ScriptInstance::Died() this->last_allocated_memory = this->GetAllocatedMemory(); // Update cache if (this->instance != nullptr) this->engine->ReleaseObject(this->instance); + delete this->instance; delete this->engine; this->instance = nullptr; this->engine = nullptr;