c++ - Local memory leak in constructor -
i intrigued when 1 static code analysis software didn't complained memory leak in constructor. inputs helpful. aware that, it's not class member. it's local pointer inside ctor
class abc { public: abc() { int *p = new int[10]; //no delete invoked... } };
you don't need static analysis tool this. gcc has ported llvm's sanitizer , it's available of gcc 4.9. it's part of clang obviously.
✿´‿`) ~/test> g++-trunk -fsanitize=undefined,address,leak -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]; ^
(✿´‿`) ~/test> ./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).
it's runtime tool, works fine situations this. of course, there's valgrind you're not going able use 2 together. disable sanitizer first before using valgrind. last not least, gdb friend.
Comments
Post a Comment