Senior Member
|
03-16-2022
, 10:44
Re: [L4D2] Shop [v4.1 | 15 March 2022]
|
#102
|
Quote:
Originally Posted by Psyk0tik
No, that's not normal. According to your handle dump, there are several query and connection handles not being closed. I checked the source code and found the issues.
Not closing queries (look for red text):
Code:
void CreateDBTable()
{
Database db = ConnectDB();
if (db != null)
{
DBResultSet query = SQL_Query(db, "CREATE TABLE IF NOT EXISTS Shop(steamId TEXT, points INTEGER, PRIMARY KEY (steamId))");
char isSucceed[255];
isSucceed = query.RowCount>0? "Success." : "Already existesd.";
if(query.RowCount>0)
{
LogMessage("[CREATE]: Create Shop table: \"%s\"", isSucceed);
}
PrintToServer("[Shop] Create Shop table: %s", isSucceed);
// query = SQL_Query(db, "DROP TABLE IF EXISTS SpecialFunctions");
query = SQL_Query(db, "CREATE TABLE IF NOT EXISTS SpecialFunctions(steamId TEXT, functionNo TEXT, isEnabled INTEGER, PRIMARY KEY (steamId, functionNo))");
isSucceed = query.RowCount>0? "Created." : "Already existesd.";
if(query.RowCount>0)
{
LogMessage("[CREATE]: Create SpecialFunctions table: \"%s\"", isSucceed);
}
PrintToServer("[Shop] Create SpecialFunctions table: %s", isSucceed);
delete query; // missing from plugin's code (v4.1)
}
delete db;
}
Not closing DB connections (look for red text):
Code:
int LoadPoints(int client)
{
Database db = ConnectDB();
int db_points = 0;
if (db != null)
{
char steamId[32];
char error[255];
Handle hSelectStmt = INVALID_HANDLE;
GetClientAuthId(client, AuthId_Steam2, steamId, sizeof(steamId));
///////////////////////// Load special functions
if ((hSelectStmt = SQL_PrepareQuery(db, "SELECT functionNo, isEnabled FROM SpecialFunctions WHERE steamId = ?", error, sizeof(error))) == INVALID_HANDLE)
{
LogError("[ERROR]: SELECT SQL_PrepareQuery: \"%s\"", error);
}
// Disabled all special function first
for(int i =0; i< SF_SIZE; i++)
{
g_bClientSF[client][0][i] = false;
g_bClientSF[client][1][i] = false;
}
SQL_BindParamString(hSelectStmt, 0, steamId, false);
if (SQL_Execute(hSelectStmt))
{
while(SQL_FetchRow(hSelectStmt))
{
char functionNo[32];
SQL_FetchString(hSelectStmt, 0, functionNo, sizeof(functionNo));
int index = GetFunctionIndex(functionNo);
bool isEnabled = SQL_FetchInt(hSelectStmt, 1) == 1;
g_bClientSF[client][0][index] = isEnabled;
g_bClientSF[client][1][index] = true;
}
}
///////////////////////////////////////////////////
////////////////////////////////// Load points
if ((hSelectStmt = SQL_PrepareQuery(db, "SELECT steamId,points FROM Shop WHERE steamId = ?", error, sizeof(error))) == INVALID_HANDLE)
{
LogError("[ERROR]: SELECT SQL_PrepareQuery: \"%s\"", error);
delete db; // missing from plugin's code (v4.1)
return db_points;
}
SQL_BindParamString(hSelectStmt, 0, steamId, false);
// Find the client in the database
if (SQL_Execute(hSelectStmt))
{
char playerName[64];
GetClientName(client, playerName, sizeof(playerName));
if(SQL_FetchRow(hSelectStmt))
{
SQL_FetchString(hSelectStmt, 0, steamId, sizeof(steamId));
db_points = SQL_FetchInt(hSelectStmt, 1);
LogAction(client, -1, "[LOAD]: \"%L\" loaded the record successfully! Points: \"%d\"", client, db_points);
}
else //INSERT
{
// if the user is not existed in the database, insert new one.
Handle hInsertStmt = INVALID_HANDLE;
if ((hInsertStmt = SQL_PrepareQuery(db, "INSERT INTO Shop(steamId, points) VALUES(?,?)", error, sizeof(error))) == INVALID_HANDLE)
{
LogError("[ERROR]: INSERT SQL_PrepareQuery: \"%s\"", error);
}
else
{
SQL_BindParamString(hInsertStmt, 0, steamId, false);
SQL_BindParamInt(hInsertStmt, 1, 0, false); // Default point is 0
if (!SQL_Execute(hInsertStmt))
{
LogError("[ERROR]: INSERT error Error: \"%s\"", error);
}
int rs = SQL_GetAffectedRows(hInsertStmt);
if(rs>0)
{
LogAction(client, -1, "[INSERT]: \"%L\" is a new user!!", client);
}else
{
LogError("[ERROR]: \"%L\" made insert error \"%s\"", client, error);
}
}
delete hInsertStmt;
}
}
delete hSelectStmt;
}
delete db;
return db_points;
}
|
Thank you for pointing up this, I have added delete statements.
Last edited by pan0s; 03-16-2022 at 10:45.
|
|