Generate an error message when core tries to assign a script's ScriptName which is already assigned.

This means if that happened the core override the older script.
As a result, the earlier one doesn't work and can't deallocate so generates memory leak.

This commit also fixes memory leaks caused by that.

Valgrind log:

==4753== 354 (312 direct, 42 indirect) bytes in 1 blocks are definitely lost in loss record 1,194 of 1,218
==4753==    at 0x4C2626C: operator new(unsigned long) (vg_replace_malloc.c:230)
==4753==    by 0x1039D85: AddSC_go_scripts() (go_scripts.cpp:1015)
==4753==    by 0xDC196F: AddScripts() (ScriptLoader.cpp:534)
==4753==    by 0xC19943: ScriptMgr::ScriptsInit() (ScriptMgr.cpp:70)
==4753==    by 0xD1CCAE: World::SetInitialWorldSettings() (World.cpp:1608)
==4753==    by 0x9100A6: Master::Run() (Master.cpp:234)
==4753==    by 0x90F5D6: main (Main.cpp:146)

--HG--
branch : trunk
This commit is contained in:
Anubisss
2010-05-13 14:15:11 +02:00
parent e7faef7e76
commit 10bf921a65
+26
View File
@@ -160,10 +160,36 @@ void Script::RegisterSelf()
{ {
int id = GetScriptId(Name.c_str()); int id = GetScriptId(Name.c_str());
if (id) if (id)
{
// try to find the script in assigned scripts
bool IsExist = false;
for (uint16 i = 0; i < MAX_SCRIPTS; ++i)
{
if (m_scripts[i])
{
// if the assigned script's name and the new script's name is the same
if (m_scripts[i]->Name == Name)
{
IsExist = true;
break;
}
}
}
// if the script doesn't assigned -> assign it!
if (!IsExist)
{ {
m_scripts[id] = this; m_scripts[id] = this;
++num_sc_scripts; ++num_sc_scripts;
} }
// if the script is already assigned -> delete it!
else
{
// TODO: write a better error message than this one :)
error_log("ScriptName: '%s' already assigned with the same ScriptName, so the script can't work.", Name.c_str());
delete this;
}
}
else else
{ {
if (Name.find("example") == std::string::npos) if (Name.find("example") == std::string::npos)