I’m creating a small solver that takes three arguments: number of hours, current time and true time.
The program itself works fine, but there are memory leaks that I am unable to fix.
Code:
Inside my main:
vector<ClockState> moves = sol.solve(curClock);
solve method:
std::vector<Game> Solver<Game>::solve(Game curCfg){
std::vector<Game> result;
std::vector<Game> seen;
std::queue<Game> q;
q.push(curCfg);
seen.push_back(curCfg);
std::vector<Game> moves;
Game cfg = q.front();
Game * newCfg = NULL;
while (q.size() > 0) {
if (q.front().isEndState()) {
break;
}
cfg = q.front();
q.pop();
cfg.getMoves(moves);
for (unsigned int i = 0; i < moves.size(); i++) {
if (find(seen.begin(), seen.end(), moves[i]) == seen.end()) {
newCfg = new Game(cfg);
moves[i].setPrev(newCfg);
q.push(moves[i]);
seen.push_back(moves[i]);
}
}
}
delete newCfg;
if(q.empty()){
return result;
}
Game temp(q.front());
while (true) {
result.push_back(temp);
if (temp == curCfg) {
break;
}
temp = temp.prev();
}
reverse(result.begin(), result.end());
return result;
}
And my setPrev method (inside ClockState)
void ClockState::setPrev(ClockState *prev) {
previous = prev;
}
previous is a pointer inside the ClockState class.
From my understanding I need to delete newCfg and even when I try to do that it still causes a memory leak.
Here is the output from valgrind --leak-check=full ./clock 12 10 3
==16856==
==16856== HEAP SUMMARY:
==16856== in use at exit: 64 bytes in 4 blocks
==16856== total heap usage: 28 allocs, 24 frees, 2,095 bytes allocated
==16856==
==16856== 64 (16 direct, 48 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==16856== at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255)
==16856== by 0x8049AB1: Solver<ClockState>::solve(ClockState) (Solver.h:102)
==16856== by 0x804964B: main (clock.cpp:106)
==16856==
==16856== LEAK SUMMARY:
==16856== definitely lost: 16 bytes in 1 blocks
==16856== indirectly lost: 48 bytes in 3 blocks
==16856== possibly lost: 0 bytes in 0 blocks
==16856== still reachable: 0 bytes in 0 blocks
==16856== suppressed: 0 bytes in 0 blocks
==16856==
==16856== For counts of detected and suppressed errors, rerun with: -v
==16856== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 6)
Line 102 is newCfg = new Game(cfg);
When Solver returns that vector, I end up printing the results, and directly after that I delete each previous with the following:
for(int j = 0; j < moves.size(); j++){
moves[j].deletePrev();
}
deletePrev() just says delete previous;
Appreciate it.
You create new objects in aloop, but delete only one, which causes your memory leak.
You need to loop through each move and delete the element pointer to by the
previouspointerAlternatively, just create only one
Gameobject and delete it when you are done with it (as you are re-creating identical objects many times over)Yet another alternative is to copy the
Gameobject by value (so nonewordeleteis needed) if you don’t want to share the object among many other objectAnother approach is to use some sort of smart pointer (e.g. from BOOST or use C++11)