From 33566a626cdd19f941663dfc77348be919755fd9 Mon Sep 17 00:00:00 2001 From: def Date: Thu, 8 Aug 2013 01:47:49 +0200 Subject: [PATCH] Try to fix memory leaks --- src/base/system.c | 5 +++-- src/game/server/score/sql_score.cpp | 27 +++++++++++++++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/base/system.c b/src/base/system.c index 4ea492d7b..f8f7b87af 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -703,12 +703,13 @@ int net_host_lookup(const char *hostname, NETADDR *addr, int types) hints.ai_family = AF_INET6; e = getaddrinfo(host, NULL, &hints, &result); - if(e != 0 || !result) + + if(!result || e != 0) return -1; sockaddr_to_netaddr(result->ai_addr, addr); - freeaddrinfo(result); addr->port = port; + freeaddrinfo(result); return 0; } diff --git a/src/game/server/score/sql_score.cpp b/src/game/server/score/sql_score.cpp index b0aad3db7..b365db669 100644 --- a/src/game/server/score/sql_score.cpp +++ b/src/game/server/score/sql_score.cpp @@ -23,6 +23,7 @@ CSqlScore::CSqlScore(CGameContext *pGameServer) : m_pGameServer(pGameServer), m_pIp(g_Config.m_SvSqlIp), m_Port(g_Config.m_SvSqlPort) { + m_pDriver = NULL; str_copy(m_aMap, g_Config.m_SvMap, sizeof(m_aMap)); NormalizeMapname(m_aMap); @@ -87,10 +88,24 @@ CSqlScore::~CSqlScore() delete m_PointsInfos; lock_wait(gs_SqlLock); lock_release(gs_SqlLock); + + try + { + delete m_pStatement; + delete m_pConnection; + dbg_msg("SQL", "SQL connection disconnected"); + } + catch (sql::SQLException &e) + { + dbg_msg("SQL", "ERROR: No SQL connection"); + } } bool CSqlScore::Connect() { + if (m_pDriver != NULL) + return true; + try { // Create connection @@ -98,6 +113,8 @@ bool CSqlScore::Connect() char aBuf[256]; str_format(aBuf, sizeof(aBuf), "tcp://%s:%d", m_pIp, m_Port); m_pConnection = m_pDriver->connect(aBuf, m_pUser, m_pPass); + bool Reconnect = true; + m_pConnection->setClientOption("MYSQL_OPT_RECONNECT", &Reconnect); // Create Statement m_pStatement = m_pConnection->createStatement(); @@ -161,16 +178,6 @@ bool CSqlScore::Connect() void CSqlScore::Disconnect() { - try - { - delete m_pStatement; - delete m_pConnection; - dbg_msg("SQL", "SQL connection disconnected"); - } - catch (sql::SQLException &e) - { - dbg_msg("SQL", "ERROR: No SQL connection"); - } } // create tables... should be done only once