2012-04-18 108 views
1

我檢查了每一行,但無法找到我忘記刪除的位置。我在本網站上找到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) 
+1

你的代碼有內存泄漏,因爲你使用手動內存管理。不要這樣做。你用C++編程,所以使用RAII來管理資源。既然你使用動態數組'std :: vector '是你應該使用 – Grizzly 2012-04-18 15:27:02

+0

我該如何使用RAII?有小費嗎? – 2012-04-18 16:10:35

+0

你爲什麼認爲你有內存泄漏?引用你的內存泄漏工具告訴你。我們仍在等待您承諾的Valgrind輸出。該工具是否不告訴你泄漏的內存在哪裏分配? – 2012-04-18 16:16:26

回答

5

calculateAvgTime,你computers分配數組,但你只釋放一個項目。無論何時使用new[],請使用delete[]

更好的是,只需使用vector並觀察此程序的內存管理問題就會消失。

+0

對於簡單類型的指針,例如'int','char','short',沒有[]刪除就沒有問題。 – ciphor 2012-04-18 15:28:30

+3

@ciphor:不是。它是按照標準的UB,並且會在相當多的實現中崩潰。 – PlasmaHH 2012-04-18 15:32:33

+0

感謝提醒丟失的括號,我糾正了,但它仍然有內存泄漏。 – 2012-04-18 16:12:07

0

我沒有看到'rowNo'的定義在哪裏,所以可能是FreeDynamicArray()沒有刪除所有的內存,如果你正在使用VS嘗試獲得可視檢漏儀(here),它將幫助您追蹤內存泄漏情況並幫助您找出錯誤。