我的C++程序内存泄漏

My memory leak in C++ program

本文关键字:泄漏 内存 程序 C++ 我的      更新时间:2023-10-16

我面临一个新问题——内存分配和泄漏——这是我的错误日志:

Dr. Memory version 1.4.6 build 2 built on Mar  7 2012 10:14:04
Application cmdline: ""D:c++BeginLab3-5_OOPDebugLab3-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++BeginLab3-5_OOPDebug/../Controller.cpp:156]
# 1 main                           [D:c++BeginLab3-5_OOPDebug/../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++BeginLab3-5_OOPDebug/../ListStruc.cpp:24]
# 2 main                   [D:c++BeginLab3-5_OOPDebug/../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++BeginLab3-5_OOPDebug/../ListStruc.cpp:24]
# 2 main                   [D:c++BeginLab3-5_OOPDebug/../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:UsersWarzaruAppDataRoaming/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),如果我存储int(不同类型的支出),但当我使用一些函数时,我会出现Segmentation错误。我没有这种错误的经验,所以任何建议都是有用的。

编辑:

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

遵循规则"取消分配动态分配的每个内存块"

使用delete取消分配您使用new 分配的内存

这可能会给你带来一些启示http://www.cplusplus.com/doc/tutorial/dynamic/

如果使用new[]分配阵列,则使用delete[] 将其删除

在这种情况下,我建议您为struct Arr编写一个构造函数和析构函数,而不是编写普通函数。

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

因此,程序中的语句i<5所发生的情况是,内部for循环变成了一个不确定循环,并试图访问未分配的内存。

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    }

我看到你删除了[]ingloc->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    }

中的析构函数似乎很奇怪

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