c++ - 我在 C++ 程序中的内存泄漏

标签 c++ memory-leaks new-operator delete-operator

我遇到了一个与内存分配和泄漏有关的新问题,这是我的错误日志:

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

最佳答案

遵循“释放动态分配的每个内存块”的规则

使用delete 释放您使用new 分配的内存

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

如果您使用 new[] 分配了一个数组,那么使用 delete[] 删除它

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

关于c++ - 我在 C++ 程序中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10004785/

相关文章:

c++ - 如何控制(即中止)QScriptEngine 的当前评估

c - 如何正确释放哈希表上的元素

c++ - 为什么我不能使用 "delete"关键字回收动态分配的内存?

.net - 测试 .NET 应用程序中的内存泄漏

c++ - 这样的分配在 C++ 中是个好主意吗

javascript - JavaScript 中关键字 'new' 有什么副作用?

C++ 继承和运算符重载

c++ - 数论算法

c++ - 如何检查用户是否输入了有效值?

java - 已检测到内存泄漏但无法理解确切原因,因为 hashmap 条目令人困惑?