diff options
author | rubidium <rubidium@openttd.org> | 2009-02-03 22:42:42 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2009-02-03 22:42:42 +0000 |
commit | 2eebb525927a99630640f52dd51867ea8e989622 (patch) | |
tree | 4f0ff072e810fbda1bd6a01083ab7be1081bfdf3 /src | |
parent | e628c5360dbd6fe063f379aff9898920f95d340e (diff) | |
download | openttd-2eebb525927a99630640f52dd51867ea8e989622.tar.xz |
(svn r15330) -Fix [FS#2597]: leaking of Squirrel when using circular references (by enabling the GC).
Diffstat (limited to 'src')
-rw-r--r-- | src/ai/ai_core.cpp | 8 | ||||
-rw-r--r-- | src/ai/ai_instance.cpp | 5 | ||||
-rw-r--r-- | src/ai/ai_instance.hpp | 5 | ||||
-rw-r--r-- | src/script/squirrel.cpp | 5 | ||||
-rw-r--r-- | src/script/squirrel.hpp | 5 |
5 files changed, 28 insertions, 0 deletions
diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp index e9e2a5bd2..e49a7dd4b 100644 --- a/src/ai/ai_core.cpp +++ b/src/ai/ai_core.cpp @@ -3,6 +3,7 @@ /** @file ai_core.cpp Implementation of AI. */ #include "../stdafx.h" +#include "../core/bitmath_func.hpp" #include "../company_base.h" #include "../company_func.h" #include "../debug.h" @@ -68,6 +69,13 @@ } } + /* Occasionally collect garbage; every 255 ticks do one company. + * Effectively collecting garbage once every two months per AI. */ + if ((AI::frame_counter & 255) == 0) { + CompanyID cid = (CompanyID)GB(AI::frame_counter, 8, 4); + if (IsValidCompanyID(cid) && !IsHumanCompany(cid)) GetCompany(cid)->ai_instance->CollectGarbage(); + } + _current_company = OWNER_NONE; } diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp index 26d55481b..37529eeab 100644 --- a/src/ai/ai_instance.cpp +++ b/src/ai/ai_instance.cpp @@ -296,6 +296,11 @@ void AIInstance::GameLoop() } } +void AIInstance::CollectGarbage() +{ + if (this->is_started && !this->is_dead) this->engine->CollectGarbage(); +} + /* static */ void AIInstance::DoCommandReturn(AIInstance *instance) { instance->engine->InsertResult(AIObject::GetLastCommandRes()); diff --git a/src/ai/ai_instance.hpp b/src/ai/ai_instance.hpp index 688b3c133..9c1aaca59 100644 --- a/src/ai/ai_instance.hpp +++ b/src/ai/ai_instance.hpp @@ -45,6 +45,11 @@ public: void GameLoop(); /** + * Let the VM collect any garbage. + */ + void CollectGarbage(); + + /** * Get the storage of this AI. */ static class AIStorage *GetStorage(); diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp index 26196292c..1400eaf0a 100644 --- a/src/script/squirrel.cpp +++ b/src/script/squirrel.cpp @@ -175,6 +175,11 @@ bool Squirrel::Resume(int suspend) return this->vm->_suspended != 0; } +void Squirrel::CollectGarbage() +{ + sq_collectgarbage(this->vm); +} + bool Squirrel::CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend) { /* Store the stack-location for the return value. We need to diff --git a/src/script/squirrel.hpp b/src/script/squirrel.hpp index 39357bade..fc7472e96 100644 --- a/src/script/squirrel.hpp +++ b/src/script/squirrel.hpp @@ -101,6 +101,11 @@ public: */ bool Resume(int suspend = -1); + /** + * Tell the VM to do a garbage collection run. + */ + void CollectGarbage(); + void InsertResult(bool result); void InsertResult(int result); |