【问题标题】:What happens with the memory leak?内存泄漏会发生什么?
【发布时间】:2018-06-08 08:30:55
【问题描述】:

我真的不明白发生异常时在堆中分配的内存会发生什么:

#include <iostream>
#include <vector>
using namespace std;

class Base {
private:
    int *a;
public:
    Base() {
        // a = new int[100];

        throw runtime_error("err");
    }

    ~Base() {
        // delete[] a;
    }
};

int main() {
    std::vector<Base> bases;
    Base base;
    bases.push_back(base);

    std::cout << bases.size() << std::endl;

    return 0;
}

好的,现在只是抛出异常的空程序。 下一个在堆中分配内存的程序:

#include <iostream>
#include <vector>
using namespace std;

class Base {
private:
    int *a;
public:
    Base() {
        a = new int[100];

        throw runtime_error("err");
    }

    ~Base() {
        // delete[] a;
    }
};

int main() {
    std::vector<Base> bases;
    Base base;
    bases.push_back(base);

    std::cout << bases.size() << std::endl;

    return 0;
}

方案一:

==14151== HEAP SUMMARY:
==14151==     in use at exit: 72,876 bytes in 3 blocks
==14151==   total heap usage: 4 allocs, 1 frees, 72,908 bytes allocated
==14151== 
==14151== Searching for pointers to 3 not-freed blocks
==14151== Checked 116,088 bytes
==14151== 
==14151== 144 bytes in 1 blocks are possibly lost in loss record 2 of 3
==14151==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14151==    by 0x4EC641F: __cxa_allocate_exception (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==14151==    by 0x400F44: Base::Base() (in /home/twite/CLionProjects/oop_learn/driver1)
==14151==    by 0x400E25: main (in /home/twite/CLionProjects/oop_learn/driver1)
==14151== 
==14151== LEAK SUMMARY:
==14151==    definitely lost: 0 bytes in 0 blocks
==14151==    indirectly lost: 0 bytes in 0 blocks
==14151==      possibly lost: 144 bytes in 1 blocks
==14151==    still reachable: 72,732 bytes in 2 blocks

方案二:

==14171== HEAP SUMMARY:
==14171==     in use at exit: 73,276 bytes in 4 blocks
==14171==   total heap usage: 5 allocs, 1 frees, 73,308 bytes allocated
==14171== 
==14171== Searching for pointers to 4 not-freed blocks
==14171== Checked 116,096 bytes
==14171== 
==14171== 144 bytes in 1 blocks are possibly lost in loss record 2 of 4
==14171==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14171==    by 0x4EC641F: __cxa_allocate_exception (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==14171==    by 0x400F98: Base::Base() (in /home/twite/CLionProjects/oop_learn/driver2)
==14171==    by 0x400E65: main (in /home/twite/CLionProjects/oop_learn/driver2)
==14171== 
==14171== LEAK SUMMARY:
==14171==    definitely lost: 0 bytes in 0 blocks
==14171==    indirectly lost: 0 bytes in 0 blocks
==14171==      possibly lost: 144 bytes in 1 blocks
==14171==    still reachable: 73,132 bytes in 3 blocks

那么,内存泄漏在哪里? Valgrind 没有告诉我有关内存泄漏的信息,但我在第二个程序中看到了。

【问题讨论】:

  • 您在编译示例时是否关闭了优化,以确保不会意外优化?
  • 我这样编译我的程序:g++ -std=c++11 -o driver example.cpp 然后运行了 Valgrind:valgrind --leak-check=full -v ./example
  • 尝试将-O0(大写“O”后跟零)标志添加到g++ 行,然后再次运行valgrind
  • 我用这个编译器选项得到了同样的结果

标签: c++ exception memory memory-leaks valgrind


【解决方案1】:

当程序终止时,操作系统会清理它分配的所有内存 - 通常不是问题。关闭时的内存泄漏很少成为问题(除非一些至关重要的析构函数没有机会运行)。内存泄漏是一个真正的问题,当它们发生在运行时并导致您的程序内存使用无限增长(最终导致资源耗尽)时。

【讨论】:

  • 您说过,当程序终止时,操作系统会清理它分配的所有内存。但是这个程序呢:pastebin.com/3gkhXVYG 程序终止,但 Valgrind 显示内存泄漏。
  • @TwITe Valgrind 显示泄漏,因为您自己没有释放所有内存 - 因此将其留给操作系统进行清理。这是技术上的泄漏,但它是无关紧要的。
猜你喜欢
  • 2011-05-13
  • 2011-06-28
  • 1970-01-01
  • 2021-08-15
  • 2013-04-01
  • 2016-01-01
  • 1970-01-01
  • 2011-10-25
  • 1970-01-01
相关资源
最近更新 更多