2014-12-02 57 views
-3

當一個靜態代碼分析軟件沒有在構造函數中抱怨內存泄漏時,我很感興趣。任何輸入都會有幫助。請注意,它不是班級成員。它是ctor內的本地指針構造函數中的本地內存泄漏

class ABC 
{ 
    public: 
     ABC() 
     { 
      int *p = new int[10]; 
      //No delete invoked... 
     } 
}; 
+2

有時間尋找更好的靜態代碼分析軟件?目前還不清楚你問的是什麼問題。 – 2014-12-02 06:14:03

+2

所以你的問題真的是這個軟件的名字你沒有提到? – 2014-12-02 06:14:33

+0

@remyabel不完全。這是關於構造函數的行爲。 – deepdive 2014-12-02 06:30:05

回答

2

你並不真的需要這個靜態分析工具。 GCC已經移植了LLVM's sanitizer,並且自GCC 4.9起可用。這顯然也是Clang的一部分。

✿'‿`)〜/測試>克++ - 軀幹-fsanitize =未定義,地址,泄漏-std = C++ 11 TEST.CPP -g -Wall -Wextra -pedantic

test.cpp: In constructor ‘ABC::ABC()’: 
test.cpp:6:18: warning: unused variable ‘p’ [-Wunused-variable] 
      int *p = new int[10]; 
      ^

(✿'‿`)〜/測試> ./a.out

================================================================= 
==1713==ERROR: LeakSanitizer: detected memory leaks 

Direct leak of 40 byte(s) in 1 object(s) allocated from: 
    #0 0x7f2535b07919 in operator new[](unsigned long) ../../../../trunk/libsanitizer/asan/asan_new_delete.cc:62 
    #1 0x4008cb in ABC::ABC() ~/test/test.cpp:6 
    #2 0x400856 in main ~/test/test.cpp:13 
    #3 0x31a1c21d64 in __libc_start_main (/lib64/libc.so.6+0x31a1c21d64) 

SUMMARY: AddressSanitizer: 40 byte(s) leaked in 1 allocation(s). 

這是一個運行時工具,但它完美罰款這樣的情況。當然,也總是有valgrind,但你不能一起使用它們。在使用valgrind之前首先禁用消毒劑。最後但並非最不重要,gdb是你的朋友。