无法理解如何修复此内存泄漏

时间:2012-05-17 17:14:32

标签: c++ memory-leaks valgrind

我正在创建一个小解算器,它有三个参数:小时数,当前时间和真实时间。

程序本身运行正常,但是我无法修复内存泄漏。

代码:

在我的主要内容中:

vector<ClockState> moves = sol.solve(curClock);

solve方法:

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;
}

我的setPrev方法(在ClockState内)

void ClockState::setPrev(ClockState *prev) {
    previous = prev;
}

previous是ClockState类中的指针。

根据我的理解,我需要删除newCfg,即使我尝试这样做仍会导致内存泄漏。

以下是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)

第102行是newCfg = new Game(cfg);

当Solver返回该向量时,我最终打印结果,之后我直接删除了每个以前的结果:

for(int j = 0; j < moves.size(); j++){
    moves[j].deletePrev();
}

deletePrev()只是说delete previous;

欣赏它。

2 个答案:

答案 0 :(得分:4)

您可以在aloop中创建新对象,但只删除一个,这会导致内存泄漏。

您需要遍历每个移动并删除previous指针

指向的元素指针

或者,只需创建一个Game对象并在完成后将其删除(因为您多次重新创建相同的对象)

另一个替代方法是按值复制Game对象(因此不需要newdelete)如果您不想在许多其他对象之间共享对象< / p>

另一种方法是使用某种智能指针(例如来自BOOST或使用C ++ 11)

答案 1 :(得分:2)

此代码:

Game * newCfg = NULL;
while (q.size() > 0) {

    if (q.front().isEndState()) {
        break;
    }

    cfg = q.front();
    q.pop();
    std::vector<Game> moves;
    cfg.getMoves(moves);
    for (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;

声明一个名为newCfg的变量,它是一个指向Game的指针。但是,在循环中,您可以保留新的Game类新实例并将它们添加到移动容器中。你需要'删除'你添加到移动容器中的每个Game *实例。

上面的代码基本上与此代码完全相同:

int* myInts = NULL;
myInts = new Int[2];
myInts = new Int[5];
delete myInts; // <-- only deletes memory for Int[5],
               // Int[2] memory is dangling in no-mans-land