为什么这段代码有内存泄漏?

时间:2012-04-18 15:19:47

标签: c++ pointers dynamic memory-leaks

我检查了每一行,但无法找到我忘记删除的地方。我在此网站上找到了AllocateDynamicArrayFreeDynamicArray代码,并认为它是正确的。我将尽快提供valgrind输出。任何帮助将不胜感激。

template <typename T>
T **AllocateDynamicArray(int nRows, int nCols) {
    T **dynamicArray;

    dynamicArray = new T*[nRows];
    for (int i = 0; i < nRows; i++) {
        dynamicArray[i] = new T [nCols];
        for (int j = 0; j < nCols; j++) {
            dynamicArray[i][j] = 0;
        }
    }
    return dynamicArray;
}


template <typename T>
void FreeDynamicArray(T** dArray, int nRows) {
    for (int i = 0; i < nRows; i++) {
        delete[] dArray[i];
    }
    delete[] dArray;
}

int main(int argc, char* argv[]) {

    int numOfComps = 1;
    int** input = AllocateDynamicArray<int>(rowNo, 4);
    while (calculateAvgTime(false) > maxAvgTime) {
        numOfComps++;
    }
    FreeDynamicArray(input, rowNo);
    return EXIT_SUCCESS;
}


double calculateAvgTime(bool print) {
    double waitingTime = 0;
    int* computers = new int[numOfComps];
    for (int i = 0; i < numOfComps; i++) {
        computers[i] = 0;
    }
    int** infoList = AllocateDynamicArray<int>(numOfComps, 2);

    //some code related to computers and infoList

    double waitingTime /= (double) (rowNo);
    FreeDynamicArray(infoList, numOfComps);
    delete[] computers;
    return waitingTime;
} 

以下是valgrind的输出。

==21109== 
==21109== HEAP SUMMARY:
==21109==     in use at exit: 1,344 bytes in 4 blocks
==21109==   total heap usage: 72 allocs, 68 frees, 11,752 bytes allocated
==21109== 
==21109== 336 bytes in 1 blocks are definitely lost in loss record 1 of 2
==21109==    at 0x4C28D27: operator new[](unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==21109==    by 0x402159: Heap::Heap(int) (in /home/cs/c_turhan/bin/HW3/simulator/simulator)
==21109==    by 0x401977: calculateAvgTime(bool) (in /home/cs/c_turhan/bin/HW3/simulator/simulator)
==21109==    by 0x401792: main (in /home/cs/c_turhan/bin/HW3/simulator/simulator)
==21109== 
==21109== 1,008 bytes in 3 blocks are definitely lost in loss record 2 of 2
==21109==    at 0x4C28D27: operator new[](unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==21109==    by 0x402159: Heap::Heap(int) (in /home/cs/c_turhan/bin/HW3/simulator/simulator)
==21109==    by 0x401977: calculateAvgTime(bool) (in /home/cs/c_turhan/bin/HW3/simulator/simulator)
==21109==    by 0x401724: main (in /home/cs/c_turhan/bin/HW3/simulator/simulator)
==21109== 
==21109== LEAK SUMMARY:
==21109==    definitely lost: 1,344 bytes in 4 blocks
==21109==    indirectly lost: 0 bytes in 0 blocks
==21109==      possibly lost: 0 bytes in 0 blocks
==21109==    still reachable: 0 bytes in 0 blocks
==21109==         suppressed: 0 bytes in 0 blocks
==21109== 
==21109== For counts of detected and suppressed errors, rerun with: -v
==21109== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)

2 个答案:

答案 0 :(得分:5)

calculateAvgTime中,您在computers中分配一个数组,但只释放一个项目。只要您使用delete[],就可以使用new[]

更好的是,只需使用vector并观察此程序的内存管理问题就会消失。

答案 1 :(得分:0)

我没有看到&#39; rowNo&#39;已定义,因此可能存在&#34; FreeDynamicArray()&#34;不删除所有内存,如果您使用VS尝试获取可视检漏(here),它将帮助您追踪泄漏的内存并帮助您查明错误。