c++11 - Weird memory leak in Visual Studio C++ project -
i use unique_ptr. creating poiters, use code snippet:
template<typename t, typename ...args> std::unique_ptr<t> make_unique(args&& ...args) { return std::unique_ptr<t>(new t(std::forward<args>(args)...)); //line 44 } i test project memory leaks built-in memory leak checker.
i following result:
detected memory leaks! dumping objects -> my_header.h(44) : {228} normal block @ 0x008ad568, 8 bytes long. data: < > a0 cb 8a 00 01 00 00 00 my_header.h(44) : {226} normal block @ 0x008ad5b8, 8 bytes long. data: < > 00 d6 8a 00 00 00 00 00 /////////////////this error repeats many times also, use deleaker. tool says, there no memory leaks.
you shouldn't using release() unless you're handing off pointer-raw else (hopefully std::unique_ptr or std::shared_ptr, etc). instead should using reset(), or letting scope-exit destroy managed object you.
the simplest example using code demonstrates bad , good:
bad : using release()
#include <memory> namespace { template<typename t, typename ...args> std::unique_ptr<t> make_unique(args&& ...args) { return std::unique_ptr<t>(new t(std::forward<args>(args)...)); } } int main() { std::unique_ptr<int> ptr = make_unique<int>(5); ptr.release(); } valgrind output
==29012== 4 bytes in 1 blocks lost in loss record 1 of 87 ==29012== @ 0x10003b51b: malloc (vg_replace_malloc.c:303) ==29012== 0x1001cd43d: operator new(unsigned long) (in /usr/lib/libc++.1.dylib) ==29012== 0x10000147b: std::__1::unique_ptr<int, std::__1::default_delete<int> > (anonymous namespace)::make_unique<int, int>(int&&) (in ./sample c++) ==29012== 0x100001337: main (in ./sample c++) ==29012== ==29012== leak summary: ==29012== lost: 4 bytes in 1 blocks ==29012== indirectly lost: 0 bytes in 0 blocks ==29012== possibly lost: 0 bytes in 0 blocks ==29012== still reachable: 192 bytes in 6 blocks ==29012== suppressed: 35,164 bytes in 435 blocks ==29012== reachable blocks (those pointer found) not shown. ==29012== see them, rerun with: --leak-check=full --show-leak-kinds=all ==29012== ==29012== counts of detected , suppressed errors, rerun with: -v ==29012== error summary: 1 errors 1 contexts (suppressed: 17 17) good: using reset()
#include <memory> namespace { template<typename t, typename ...args> std::unique_ptr<t> make_unique(args&& ...args) { return std::unique_ptr<t>(new t(std::forward<args>(args)...)); } } int main() { std::unique_ptr<int> ptr = make_unique<int>(5); ptr.reset(); // here } valgrind output
==29045== heap summary: ==29045== in use @ exit: 35,356 bytes in 441 blocks ==29045== total heap usage: 508 allocs, 67 frees, 41,216 bytes allocated ==29045== ==29045== leak summary: ==29045== lost: 0 bytes in 0 blocks ==29045== indirectly lost: 0 bytes in 0 blocks ==29045== possibly lost: 0 bytes in 0 blocks ==29045== still reachable: 192 bytes in 6 blocks ==29045== suppressed: 35,164 bytes in 435 blocks ==29045== reachable blocks (those pointer found) not shown. ==29045== see them, rerun with: --leak-check=full --show-leak-kinds=all ==29045== ==29045== counts of detected , suppressed errors, rerun with: -v ==29045== error summary: 0 errors 0 contexts (suppressed: 17 17) the same output above (the good) received removing second line in main() entirely.
int main() { std::unique_ptr<int> ptr = make_unique<int>(5); } and likewise this:
int main() { std::unique_ptr<int> ptr = make_unique<int>(5); std::unique_ptr<int> other(ptr.release()); } best of luck.
Comments
Post a Comment