Permalink
Browse files

Prevent some memory leaks

  • Loading branch information...
Bara committed Jan 10, 2019
1 parent 70a31fa commit 08bfea2e7675c234f4be14ca168b56467142e702
@@ -54,13 +54,16 @@ public void OnMapStart()
}

KeyValues kvConfig = new KeyValues("TTT-Models");
g_smModels = new StringMap();

if (!kvConfig.ImportFromFile(sFile))
{
delete kvConfig;
SetFailState("[TTT-Models] Something went wrong with: \"%s\"", sFile);
return;
}

delete g_smModels;
g_smModels = new StringMap();

if (kvConfig.JumpToKey("Models"))
{
@@ -10,18 +10,12 @@

char g_sFile[PLATFORM_MAX_PATH + 1];

enum Item
{
String:Short[16],
Percent,
String:Flag[16]
}
StringMap g_smPercent = null;
StringMap g_smFlag = null;

ConVar g_cPercents = null;
ConVar g_cFlags = null;

ArrayList g_aItems = null;

public Plugin myinfo =
{
name = PLUGIN_NAME,
@@ -48,21 +42,22 @@ public int Native_GetItemDiscount(Handle plugin, int numParams)

if (TTT_IsClientValid(client))
{
int iItem[Item];
for (int i = 0; i < g_aItems.Length; i++)
char sFlag[16];
g_smFlag.GetString(sItem, sFlag, sizeof(sFlag));

int iPercent = 0;

if (!HasFlags(client, sFlag))
{
g_aItems.GetArray(i, iItem[0]);

if (StrEqual(iItem[Short], sItem, false))
{
if (!HasFlags(client, iItem[Flag]))
{
return 0;
}

return iItem[Percent];
}
return iPercent;
}

if (g_smPercent.GetValue(sItem, iPercent))
{
return iPercent;
}

return iPercent;
}

return -1;
@@ -81,7 +76,11 @@ public void OnPluginStart()
TTT_EndConfig();

BuildPath(Path_SM, g_sFile, sizeof(g_sFile), "configs/ttt/shop_discounts.ini");
g_aItems = new ArrayList(18);

delete g_smPercent;
delete g_smFlag;
g_smPercent = new StringMap();
g_smFlag = new StringMap();
}

public void TTT_OnLatestVersion(const char[] version)
@@ -133,11 +132,8 @@ public void OnConfigsExecuted()

if (strlen(sShort) > 1 && iPercent >= 1 && iPercent <= 100)
{
int iItem[Item];
Format(iItem[Short], sizeof(sShort), "%s", sShort);
iItem[Percent] = iPercent;
Format(iItem[Flag], sizeof(sFlag), "%s", sFlag);
g_aItems.PushArray(iItem[0]);
g_smPercent.SetValue(sShort, iPercent, true);
g_smFlag.SetString(sShort, sFlag, true);
}
}
while (kvRules.GotoNextKey());
@@ -146,22 +142,18 @@ public void OnConfigsExecuted()
delete hFile;
}

public Action TTT_OnItemPurchase(int client, int &price, bool &count, const char[] item)
public Action TTT_OnItemPurchase(int client, int &price, bool &count, const char[] sItem)
{
if (TTT_IsClientValid(client) && IsPlayerAlive(client))
{
int iItem[Item];
for (int i = 0; i < g_aItems.Length; i++)
int iPercent = 0;
if (g_smPercent.GetValue(sItem, iPercent))
{
g_aItems.GetArray(i, iItem[0]);
if (StrEqual(iItem[Short], item, false) && HasFlags(client, iItem[Flag]))
{
float fPercentage = iItem[Percent] / 100.0;
int iDiscount = RoundToCeil(price * fPercentage);
int iOld = price;
price = iOld - iDiscount;
return Plugin_Changed;
}
float fPercentage = iPercent / 100.0;
int iDiscount = RoundToCeil(price * fPercentage);
int iOld = price;
price = iOld - iDiscount;
return Plugin_Changed;
}
}
return Plugin_Continue;
@@ -93,11 +93,17 @@ void SQL_Start()
kvDatabase.SetString("database", "ttt");
kvDatabase.SetString("user", "root");

// Delete g_dDatabase to prevent memory leaks
delete g_dDatabase;

char sError[255];
g_dDatabase = SQL_ConnectCustom(kvDatabase, sError, sizeof(sError), true);

delete kvDatabase;

if (strlen(sError) > 1)
{
delete g_dDatabase; // Delete Database handle when we've a error
LogError("(SQL_Start) Error: %s", sError);
}

@@ -119,12 +125,16 @@ void SQL_Start()
if (!SQL_CheckConfig(sEntry) && g_iRetries < g_cRetries.IntValue)
{
LogError("(SQL_Start) Couldn't find an \"%s\"-entry in your databases.cfg...", sEntry);

// Delete g_dDatabase to prevent memory leaks
delete g_dDatabase;

char sError[255];
g_dDatabase = SQL_Connect(sEntry, true, sError, sizeof(sError));

if (strlen(sError) > 1)
{
delete g_dDatabase; // Delete Database handle when we've a error
LogError("(SQL_Start) Error: %s", sError);
}

@@ -167,7 +177,12 @@ public void OnConnect(Database db, const char[] error, any data)
return;
}

g_dDatabase = db;
delete g_dDatabase;

if (db != null)
{
g_dDatabase = db;
}

CheckAndCreateTables();
}
@@ -235,11 +250,11 @@ public void SQL_QueryCB(Database db, DBResultSet results, const char[] error, Da
pack.Reset();
char sBuffer[128];
pack.ReadString(sBuffer, sizeof(sBuffer));
delete pack;

LogError("[TTT] (%s) Query failed: %s", sBuffer, error);
return;
}

delete pack;
}

public int Native_GetConnectionType(Handle plugin, int numParams)

0 comments on commit 08bfea2

Please sign in to comment.