Skip to content

Commit

Permalink
Improve Database API to avoid memory leaks in the future, part 3.
Browse files Browse the repository at this point in the history
This commit reworks SqlQueryHolder to return a unique_ptr instead of a
raw pointer to the query's result in its API.
  • Loading branch information
evil-at-wow committed Oct 21, 2023
1 parent 940fc26 commit ca17445
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 196 deletions.
11 changes: 6 additions & 5 deletions src/framework/Utilities/Callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define MANGOS_CALLBACK_H

#include <functional>
#include <memory>

/// ---------- QUERY CALLBACKS -----------

Expand All @@ -33,7 +34,7 @@ namespace MaNGOS

virtual ~IQueryCallback() = default;
virtual void Execute() = 0;
virtual void SetResult(QueryResult* queryResult) = 0;
virtual void SetResult(std::unique_ptr<QueryResult> queryResult) = 0;
};

class QueryCallback : public IQueryCallback
Expand All @@ -54,18 +55,18 @@ namespace MaNGOS

void Execute() override
{
m_Callback(m_QueryResult);
m_Callback(m_QueryResult.release());
}

void SetResult(QueryResult* queryResult) override
void SetResult(std::unique_ptr<QueryResult> queryResult) override
{
m_QueryResult = queryResult;
m_QueryResult = std::move(queryResult);
}

private:

std::function<void(QueryResult*)> m_Callback;
QueryResult* m_QueryResult;
std::unique_ptr<QueryResult> m_QueryResult;
};
}

Expand Down
4 changes: 1 addition & 3 deletions src/game/Achievements/AchievementMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ void AchievementMgr::SaveToDB()
}
}

void AchievementMgr::LoadFromDB(QueryResult* achievementResult, QueryResult* criteriaResult)
void AchievementMgr::LoadFromDB(std::unique_ptr<QueryResult> achievementResult, std::unique_ptr<QueryResult> criteriaResult)
{
// Note: this code called before any character data loading so don't must triggering any events req. inventory/etc
// all like cases must be happens in CheckAllAchievementCriteria called after character data load
Expand All @@ -648,7 +648,6 @@ void AchievementMgr::LoadFromDB(QueryResult* achievementResult, QueryResult* cri
ca.changed = false;
}
while (achievementResult->NextRow());
delete achievementResult;
}

if (criteriaResult)
Expand Down Expand Up @@ -704,7 +703,6 @@ void AchievementMgr::LoadFromDB(QueryResult* achievementResult, QueryResult* cri
}
}
while (criteriaResult->NextRow());
delete criteriaResult;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/game/Achievements/AchievementMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Entities/ObjectGuid.h"

#include <map>
#include <memory>

struct AchievementEntry;
struct AchievementCriteriaEntry;
Expand Down Expand Up @@ -280,7 +281,7 @@ class AchievementMgr

void Reset();
static void DeleteFromDB(ObjectGuid guid);
void LoadFromDB(QueryResult* achievementResult, QueryResult* criteriaResult);
void LoadFromDB(std::unique_ptr<QueryResult> achievementResult, std::unique_ptr<QueryResult> criteriaResult);
void SaveToDB();
void ResetAchievementCriteria(AchievementCriteriaTypes type, uint32 miscvalue1 = 0, uint32 miscvalue2 = 0);
void StartTimedAchievementCriteria(AchievementCriteriaTypes type, uint32 timedRequirementId, time_t startTime = 0);
Expand Down
3 changes: 1 addition & 2 deletions src/game/Entities/CharacterHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,14 +737,13 @@ void WorldSession::HandlePlayerLogin(LoginQueryHolder* holder)
SendOfflineNameQueryResponses();

// QueryResult *result = CharacterDatabase.PQuery("SELECT guildid,rank FROM guild_member WHERE guid = '%u'",pCurrChar->GetGUIDLow());
QueryResult* resultGuild = holder->GetResult(PLAYER_LOGIN_QUERY_LOADGUILD);
auto resultGuild = holder->GetResult(PLAYER_LOGIN_QUERY_LOADGUILD);

if (resultGuild)
{
Field* fields = resultGuild->Fetch();
pCurrChar->SetInGuild(fields[0].GetUInt32());
pCurrChar->SetRank(fields[1].GetUInt32());
delete resultGuild;
}
else if (pCurrChar->GetGuildId()) // clear guild related fields in case wrong data about nonexistent membership
{
Expand Down
Loading

0 comments on commit ca17445

Please sign in to comment.