Fix for possible memory leak when spawning bots

This commit is contained in:
Uleat 2018-02-19 07:34:43 -05:00
parent c36a1cd6dc
commit 765ee5eeed
4 changed files with 39 additions and 15 deletions

View File

@ -3053,7 +3053,7 @@ void Bot::Depop() {
NPC::Depop(false);
}
void Bot::Spawn(Client* botCharacterOwner) {
bool Bot::Spawn(Client* botCharacterOwner) {
if(GetBotID() > 0 && _botOwnerCharacterID > 0 && botCharacterOwner && botCharacterOwner->CharacterID() == _botOwnerCharacterID) {
// Rename the bot name to make sure that Mob::GetName() matches Mob::GetCleanName() so we dont have a bot named "Jesuschrist001"
strcpy(name, GetCleanName());
@ -3097,7 +3097,11 @@ void Bot::Spawn(Client* botCharacterOwner) {
this->SendWearChange(materialFromSlot);
}
}
return true;
}
return false;
}
// Deletes the inventory record for the specified item from the database for this bot.
@ -3246,16 +3250,20 @@ void Bot::LoadAndSpawnAllZonedBots(Client* botOwner) {
if(!ActiveBots.empty()) {
for(std::list<uint32>::iterator itr = ActiveBots.begin(); itr != ActiveBots.end(); ++itr) {
Bot* activeBot = Bot::LoadBot(*itr);
if (!activeBot)
continue;
if(activeBot) {
activeBot->Spawn(botOwner);
g->UpdatePlayer(activeBot);
// follow the bot owner, not the group leader we just zoned with our owner.
if(g->IsGroupMember(botOwner) && g->IsGroupMember(activeBot))
activeBot->SetFollowID(botOwner->GetID());
if (!activeBot->Spawn(botOwner)) {
safe_delete(activeBot);
continue;
}
if(activeBot && !botOwner->HasGroup())
g->UpdatePlayer(activeBot);
// follow the bot owner, not the group leader we just zoned with our owner.
if (g->IsGroupMember(botOwner) && g->IsGroupMember(activeBot))
activeBot->SetFollowID(botOwner->GetID());
if(!botOwner->HasGroup())
database.SetGroupID(activeBot->GetCleanName(), 0, activeBot->GetBotID());
}
}

View File

@ -275,7 +275,7 @@ public:
static bool IsValidRaceClassCombo(uint16 r, uint8 c);
bool IsValidName();
static bool IsValidName(std::string& name);
void Spawn(Client* botCharacterOwner);
bool Spawn(Client* botCharacterOwner);
virtual void SetLevel(uint8 in_level, bool command = false);
virtual void FillSpawnStruct(NewSpawn_Struct* ns, Mob* ForWho);
virtual bool Process();

View File

@ -5049,7 +5049,11 @@ void bot_subcommand_bot_spawn(Client *c, const Seperator *sep)
return;
}
my_bot->Spawn(c);
if (!my_bot->Spawn(c)) {
c->Message(m_fail, "Failed to spawn bot '%s' (id: %i)", bot_name.c_str(), bot_id);
safe_delete(my_bot);
return;
}
static const char* bot_spawn_message[16] = {
"A solid weapon is my ally!", // WARRIOR / 'generic'
@ -5805,18 +5809,22 @@ void bot_subcommand_botgroup_load(Client *c, const Seperator *sep)
return;
}
if (!leader_id) {
c->Message(m_fail, "Can not locate bot-group leader id for '%s'", botgroup_name_arg.c_str());
c->Message(m_fail, "Cannot locate bot-group leader id for '%s'", botgroup_name_arg.c_str());
return;
}
auto botgroup_leader = Bot::LoadBot(leader_id);
if (!botgroup_leader) {
c->Message(m_fail, "Could not spawn bot-group leader for '%s'", botgroup_name_arg.c_str());
c->Message(m_fail, "Could not load bot-group leader for '%s'", botgroup_name_arg.c_str());
safe_delete(botgroup_leader);
return;
}
botgroup_leader->Spawn(c);
if (!botgroup_leader->Spawn(c)) {
c->Message(m_fail, "Could not spawn bot-group leader %s for '%s'", botgroup_leader->GetName(), botgroup_name_arg.c_str());
safe_delete(botgroup_leader);
return;
}
Group* group_inst = new Group(botgroup_leader);
@ -5835,7 +5843,12 @@ void bot_subcommand_botgroup_load(Client *c, const Seperator *sep)
return;
}
botgroup_member->Spawn(c);
if (!botgroup_member->Spawn(c)) {
c->Message(m_fail, "Could not spawn bot '%s' (id: %i)", botgroup_member->GetName(), member_iter);
safe_delete(botgroup_member);
return;
}
Bot::AddBotToGroup(botgroup_member, group_inst);
}

View File

@ -273,7 +273,10 @@ bool BotDatabase::LoadBotID(const uint32 owner_id, const std::string& bot_name,
if (!owner_id || bot_name.empty())
return false;
query = StringFormat("SELECT `bot_id` FROM `bot_data` WHERE `name` = '%s' LIMIT 1", bot_name.c_str());
query = StringFormat(
"SELECT `bot_id` FROM `bot_data` WHERE `owner_id` = '%u' AND `name` = '%s' LIMIT 1",
owner_id, bot_name.c_str()
);
auto results = QueryDatabase(query);
if (!results.Success())
return false;