2012-04-04 82 views
1

我面臨的一個新問題,一些與內存分配和泄漏這裏是我的錯誤日誌:我的內存泄漏在C++程序

Dr. Memory version 1.4.6 build 2 built on Mar 7 2012 10:14:04 
Application cmdline: ""D:\c++\Begin\Lab3-5_OOP\Debug\Lab3-5_OOP.exe"" 
Recorded 62 suppression(s) from default C:\Program Files (x86)\Dr. Memory/bin/suppress-default.txt 

Error #1: UNINITIALIZED READ: reading register eax 
# 0 _fu89___ZSt4cout    [D:\c++\Begin\Lab3-5_OOP\Debug/../Controller.cpp:156] 
# 1 main       [D:\c++\Begin\Lab3-5_OOP\Debug/../M.cpp:25] 
Note: @0:00:00.924 in thread 4584 
Note: instruction: test %eax %eax 

Error #2: LEAK 12 direct bytes 0x00531420-0x0053142c + 1024 indirect bytes 
# 0 libstdc++-6.dll!Znwj   
# 1 constr()    [D:\c++\Begin\Lab3-5_OOP\Debug/../ListStruc.cpp:24] 
# 2 main     [D:\c++\Begin\Lab3-5_OOP\Debug/../M.cpp:18] 

Error #3: LEAK 12 direct bytes 0x009bec48-0x009bec54 + 1024 indirect bytes 
# 0 libstdc++-6.dll!Znwj +0x23  (0x6fcbb523 <libstdc++-6.dll+0x7b523>) 
# 1 constr()    [D:\c++\Begin\Lab3-5_OOP\Debug/../ListStruc.cpp:24] 
# 2 main     [D:\c++\Begin\Lab3-5_OOP\Debug/../M.cpp:20] 

DUPLICATE ERROR COUNTS: 

SUPPRESSIONS USED: 

ERRORS FOUND: 
     0 unique,  0 total unaddressable access(es) 
     1 unique,  1 total uninitialized access(es) 
     0 unique,  0 total invalid heap argument(s) 
     0 unique,  0 total warning(s) 
     2 unique,  2 total, 2072 byte(s) of leak(s) 
     0 unique,  0 total,  0 byte(s) of possible leak(s) 
ERRORS IGNORED: 
    78 still-reachable allocation(s) 
     (re-run with "-show_reachable" for details) 
Details: C:\Users\Warzaru\AppData\Roaming/Dr. Memory/DrMemory-Lab3-5_OOP.exe.10024.000/results.txt 

結構:

const int days=31; 
const int exp=6; 

struct Arr{ 
    int days; 
    int exp; 
    int **M; 
}; 
typedef Arr* Array; 

構造:

void constr(Array &loc){ 
    //Construct of 31*6 Matrix, were 31 nr. of days and 6 specific types: 
    //0-HouseKeeping, 1-Food, 2-Transport, 3-Clothing, 4-TelNet, 5-others 
    loc=new Arr; 
    loc->days = days; 
    loc->exp = exp; 
    loc->M = new int*[loc->days]; 
    for(int i=0; i<loc->days;i++){ 
     loc->M[i] = new int[loc->exp]; 
     for (int j = 0; j < loc->exp; j++){ 
      loc->M[i][j] = 0; 
     } 
    } 
} 

程序錯誤我只對一些功能ti例如功能:

void maxDay(Array &M){ 
    //Output the day with highest value 
    cout<<"test"; 
    int hD = 0; 
    int s1 = 0; 
    int s2 = 0; 
    cout<<"test"; 
    for(int i = 0; i<30;i++){ 
     s1=0; 
     for (int j=0; i<5; j++){ 
      s1 = s1 + M->M[i][j]; 
      if(s2 <= s1){ 
       s2 = s1; 
       hD = i; 
       cout<<"test"; 
      } 
     } 
    } 

} 

這麼短,我有一個結構Arr(矩陣31 * 6)是我存儲整數(不同類型的開銷),但是當我使用我的一些函數時,我得到分段錯誤。我沒有這種錯誤的經驗,所以任何建議都很有用。

編輯:

void destruc(Array &loc){ 
    for(int i=0; i<loc->days;i++){ 
     delete[] loc->M[i]; 
     for (int j = 0; j < loc->exp; j++){ 
      delete[] loc->M[i][j]; 
    } 
} 
} 
+3

我看到'new',而不是'delete'。 – Pubby 2012-04-04 04:12:01

+2

我建議你用'std :: vector >'替換'int ** M'。實際上,有更好的方法,但這對現有解決方案影響最小。 – 2012-04-04 04:12:30

+0

事情是我必須使用我自己的結構:| – 2012-04-04 04:21:45

回答

0

在析構函數似乎是陌生

void destruc(Array &loc){ 
    for(int i=0; i<loc->days;i++){ 
     delete[] loc->M[i]; <-------------- deleting array of pointers to array 
     for (int j = 0; j < loc->exp; j++){ 
      delete[] loc->M[i][j]; <------- deleting pointer to array that 
              is already deallocated 
    } 
} 

析構函數應該看看下面的方式(根據構造器):

void destruc(Array &loc){ 
    for(int i=0; i<loc->days;i++){ 
     delete[] loc->M[i]; 
    } 
    delete[] M; 
} 
1

遵循規則「取消分配您已經動態分配的內存每塊」使用

解除分配delete使用new

這可能會引發一些已分配的內存光爲你http://www.cplusplus.com/doc/tutorial/dynamic/

如果你使用分配一個數組然後刪除它使用delete[]

在這種情況下,我會建議你寫struct Arr的構造函數和析構函數,而不是編寫正常的函數。

+0

這是正確的嗎?我編輯了psot並添加了解構器。 – 2012-04-04 04:29:14

+1

你正在混合使用'new'和'new []'delete []'和'delete'' – 2012-04-04 04:39:49

1
// I hope you intended to write j<5 
for (int j=0; i<5; j++){ //infinite Loop... as j is still 0 

所以在你的程序中聲明i<5發生的事情是,你內心的循環將成爲無限循環並嘗試訪問未分配的內存。

+0

好點!它一定是一個錯字 – 2012-04-04 04:49:10

1
1 void destruc(Array &loc) { 
2  for(int i=0; i<loc->days;i++) { 
3   delete[] loc->M[i]; 
4   for (int j = 0; j < loc->exp; j++) { 
5    delete[] loc->M[i][j]; 
6   } 
7  } 
8 } 

我看到你刪除[]'荷蘭國際集團loc-> M [I](3號線),但你仍然在第5行

引用它的內容,我認爲這是一個錯誤的您已將內存交回堆中,現在應用程序的任何其他部分都可以重新使用它。所以,當你的應用程序到達第5行時,它可能沒有你期望的內容。

我建議重寫它作爲...

1 void destruc(Array &loc) { 
2  for(int i=0; i<loc->days;i++) { 
3   for (int j = 0; j < loc->exp; j++) { 
4    delete[] loc->M[i][j]; 
5   } 
6   delete[] loc->M[i]; 
7  } 
8 }