From 0f364bf00a1b3d90e83de0d86df0ca95c625439b Mon Sep 17 00:00:00 2001 From: VladimirMangos Date: Wed, 5 Aug 2009 20:21:25 +0400 Subject: [PATCH] [8318] Deleted as expected runnable objects at related Thread deleting for avoid memory leaks. --- src/mangosd/Master.cpp | 26 +++++++++++++++++++------- src/realmd/AuthSocket.cpp | 4 ++-- src/shared/Database/Database.h | 2 +- src/shared/Database/DatabaseMysql.cpp | 4 ++-- src/shared/Threading.cpp | 5 ++++- src/shared/Threading.h | 2 +- src/shared/revision_nr.h | 2 +- 7 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/mangosd/Master.cpp b/src/mangosd/Master.cpp index 14b13d87b..676b4297e 100644 --- a/src/mangosd/Master.cpp +++ b/src/mangosd/Master.cpp @@ -223,12 +223,14 @@ int Master::Run() _HookSignals(); ///- Launch WorldRunnable thread - ACE_Based::Thread t(*new WorldRunnable); + ACE_Based::Thread t(new WorldRunnable); t.setPriority(ACE_Based::Highest); // set server online loginDatabase.PExecute("UPDATE realmlist SET color = 0, population = 0 WHERE id = '%d'",realmID); + ACE_Based::Thread* cliThread = NULL; + #ifdef WIN32 if (sConfig.GetBoolDefault("Console.Enable", true) && (m_ServiceStatus == -1)/* need disable console in service mode*/) #else @@ -236,10 +238,10 @@ int Master::Run() #endif { ///- Launch CliRunnable thread - ACE_Based::Thread td1(*new CliRunnable); + cliThread = new ACE_Based::Thread(new CliRunnable); } - ACE_Based::Thread td2(*new RARunnable); + ACE_Based::Thread td2(new RARunnable); ///- Handle affinity for multiple processors and process priority on Windows #ifdef WIN32 @@ -300,7 +302,7 @@ int Master::Run() { FreezeDetectorRunnable *fdr = new FreezeDetectorRunnable(); fdr->SetDelayTime(freeze_delay*1000); - ACE_Based::Thread t(*fdr); + ACE_Based::Thread t(fdr); t.setPriority(ACE_Based::Highest); } @@ -338,9 +340,10 @@ int Master::Run() sLog.outString( "Halting process..." ); - #ifdef WIN32 - if (sConfig.GetBoolDefault("Console.Enable", true)) + if(cliThread) { + #ifdef WIN32 + // this only way to terminate CLI thread exist at Win32 (alt. way exist only in Windows Vista API) //_exit(1); // send keyboard input to safely unblock the CLI thread @@ -375,8 +378,17 @@ int Master::Run() b[3].Event.KeyEvent.wRepeatCount = 1; DWORD numb; BOOL ret = WriteConsoleInput(hStdIn, b, 4, &numb); + + cliThread->wait(); + + #else + + cliThread->destroy(); + + #endif + + delete cliThread; } - #endif // for some unknown reason, unloading scripts here and not in worldrunnable // fixes a memory leak related to detaching threads from the module diff --git a/src/realmd/AuthSocket.cpp b/src/realmd/AuthSocket.cpp index 5611d85e8..02e6aad80 100644 --- a/src/realmd/AuthSocket.cpp +++ b/src/realmd/AuthSocket.cpp @@ -926,7 +926,7 @@ bool AuthSocket::_HandleXferResume() ibuf.Read((char*)&start,sizeof(start)); fseek(pPatch, start, 0); - ACE_Based::Thread u(*new PatcherRunnable(this)); + ACE_Based::Thread u(new PatcherRunnable(this)); return true; } @@ -959,7 +959,7 @@ bool AuthSocket::_HandleXferAccept() ibuf.Remove(1); // clear input buffer fseek(pPatch, 0, 0); - ACE_Based::Thread u(*new PatcherRunnable(this)); + ACE_Based::Thread u(new PatcherRunnable(this)); return true; } diff --git a/src/shared/Database/Database.h b/src/shared/Database/Database.h index da3e9a706..d7dfd23e2 100644 --- a/src/shared/Database/Database.h +++ b/src/shared/Database/Database.h @@ -39,7 +39,7 @@ class MANGOS_DLL_SPEC Database TransactionQueues m_tranQueues; ///< Transaction queues from diff. threads QueryQueues m_queryQueues; ///< Query queues from diff threads - SqlDelayThread* m_threadBody; ///< Pointer to delay sql executer + SqlDelayThread* m_threadBody; ///< Pointer to delay sql executer (owned by m_delayThread) ACE_Based::Thread* m_delayThread; ///< Pointer to executer thread public: diff --git a/src/shared/Database/DatabaseMysql.cpp b/src/shared/Database/DatabaseMysql.cpp index 1a56c1b64..969617272 100644 --- a/src/shared/Database/DatabaseMysql.cpp +++ b/src/shared/Database/DatabaseMysql.cpp @@ -424,8 +424,8 @@ void DatabaseMysql::InitDelayThread() assert(!m_delayThread); //New delay thread for delay execute - m_threadBody = new MySQLDelayThread(this); - m_delayThread = new ACE_Based::Thread(*m_threadBody); + m_threadBody = new MySQLDelayThread(this); // will deleted at m_delayThread delete + m_delayThread = new ACE_Based::Thread(m_threadBody); } void DatabaseMysql::HaltDelayThread() diff --git a/src/shared/Threading.cpp b/src/shared/Threading.cpp index 496e86353..25bd3120c 100644 --- a/src/shared/Threading.cpp +++ b/src/shared/Threading.cpp @@ -101,7 +101,7 @@ Thread::Thread() : m_task(0), m_iThreadId(0), m_hThreadHandle(0) } -Thread::Thread(Runnable& instance) : m_task(&instance), m_iThreadId(0), m_hThreadHandle(0) +Thread::Thread(Runnable* instance) : m_task(instance), m_iThreadId(0), m_hThreadHandle(0) { bool _start = start(); ASSERT (_start); @@ -110,6 +110,9 @@ Thread::Thread(Runnable& instance) : m_task(&instance), m_iThreadId(0), m_hThrea Thread::~Thread() { //Wait(); + + // deleted runnable object (owned by Thread) + delete m_task; } //initialize Thread's class static member diff --git a/src/shared/Threading.h b/src/shared/Threading.h index eac3c0e8e..ab423696c 100644 --- a/src/shared/Threading.h +++ b/src/shared/Threading.h @@ -61,7 +61,7 @@ namespace ACE_Based { public: Thread(); - Thread(Runnable& instance); + explicit Thread(Runnable* instance); ~Thread(); bool start(); diff --git a/src/shared/revision_nr.h b/src/shared/revision_nr.h index 535b25dd8..089166a68 100644 --- a/src/shared/revision_nr.h +++ b/src/shared/revision_nr.h @@ -1,4 +1,4 @@ #ifndef __REVISION_NR_H__ #define __REVISION_NR_H__ - #define REVISION_NR "8317" + #define REVISION_NR "8318" #endif // __REVISION_NR_H__