Breina
Breina

Reputation: 537

Seems to be a memory leak in this little piece of code

GetPlayerName gets the name of a player out of the memory. GetPlayerId finds the player's id in the database, and if he's not found, inserts him. It always returns an integer.

Whenever I pass the playerIDs line, sometimes my memory usage (as shown by taskmgr) jumps up by 4, or a multiple of that. It never seems to go back down, even when I go out of the scope of this function. The location of both playerIDs and playerName is always the same.

I'm clueless to why this is. If anyone could point me to some direction, that would be awesome.

Thank you in advance,

CX

int playerIDs[MAXPLAYERS];
for (int i = 0; i < gd.GetPlayerAmount(); i++)
{
    string playerName = gd.GetPlayerName(i);
    playerIDs[i] = GetPlayerId(playerName);
}

Upvotes: 0

Views: 122

Answers (3)

Breina
Breina

Reputation: 537

It seemed to be I had forgotten an

PQclear(res);

Deeper in the code, I didn't debug far enough.

Upvotes: 0

bitmask
bitmask

Reputation: 34618

There is no possible memory leak caused by the code you showed, but possibly in or due to the functions you call. Ask valgrind to corner the issue.

Upvotes: 3

derpface
derpface

Reputation: 1691

Assuming string is std::string, there's nothing leaky about this code. The problem must be inside one of the functions called. I'm guessing GetPlayerID since the leak is in multiples of 4. Also, there are better memory leak tracking tools than using your Task Manager. What IDE are you using?

If you're in MSVC, you can turn on some quick simple memory leak detection by adding the following:

#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>


/******************* This goes at the top of main() *******************/
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetBreakAlloc(-1L);
/***********************************************************/

Upvotes: 1

Related Questions