2017-12-27 438 views
3

我真不明白,當異常發生在堆與分配的內存會發生什麼:內存泄漏會發生什麼?

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

計劃1:

==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 

方案2:

==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沒有告訴我有關內存泄漏的情況,但是我看到了第二個程序中的那個。

+2

編譯示例時是否關閉了優化,以確保事物不會意外優化? – dasblinkenlight

+0

我編譯我的程序是這樣的:g ++ -std = C++ 11 -o driver example.cpp然後運行Valgrind: valgrind --leak-check = full -v ./example – TwITe

+0

嘗試添加「-O0」(大寫「O」後跟零)標誌到'g ++'行,然後再次運行'valgrind'。 – dasblinkenlight

回答

1

當程序終止時,操作系統清理它分配的任何內存 - 通常不是問題。內存泄漏在關機時很少成爲問題(除非一些重要的析構函數沒有機會運行)。如果內存泄漏是真正的問題,那麼當它們在運行時發生並導致程序內存使用增長無限(最終導致資源耗盡)時。

+0

你說當程序終止時,操作系統清理它分配的任何內存。但是這個程序怎麼樣:https://pastebin.com/3gkhXVYG 程序終止,但Valgrind顯示內存泄漏。 – TwITe

+1

@TwITe Valgrind顯示泄漏,因爲您沒有釋放所有內存 - 因此將其留給操作系統進行清理。這在技術上*是泄漏,但這是無關緊要的。 –