静态shared_ptr中的内存泄漏

时间:2016-10-31 20:29:51

标签: c++ memory-leaks shared-ptr

由于某种原因我无法理解在破坏静态shared_ptr时我想到的内存泄漏。该程序目前运行完美,直到结束,但是在我得到的最后一个返回语句之后:

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

我在计划上运行了Valgrind并得到了:

==4185== Invalid read of size 4
==4185==    at 0x4559DC: __gnu_cxx::__exchange_and_add_single(int*, int) (atomicity.h:67)
==4185==    by 0x455AC5: __gnu_cxx::__exchange_and_add_dispatch(int*, int) (atomicity.h:84)
==4185==    by 0x470D6C: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:141)
==4185==    by 0x46AB12: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() (shared_ptr_base.h:546)
==4185==    by 0x4A1B03: std::__shared_ptr<md::sym::Registry, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() (shared_ptr_base.h:781)
==4185==    by 0x4A1B41: std::shared_ptr<md::sym::Registry>::~shared_ptr() (shared_ptr.h:93)
==4185==    by 0x78F45E9: __cxa_finalize (cxa_finalize.c:56)
==4185==    by 0x5B10E62: ??? (in /home/alex/.CLion2016.2/system/cmake/generated/metadiff-core-dac47eeb/dac47eeb/Debug/libmd_core.so)
==4185==    by 0x4010739: _dl_fini (dl-fini.c:252)
==4185==    by 0x78F4258: __run_exit_handlers (exit.c:82)
==4185==    by 0x78F42A4: exit (exit.c:104)
==4185==    by 0x78D9ECB: (below main) (libc-start.c:321)
==4185==  Address 0x7c95ab8 is 8 bytes inside a block of size 96 free'd
==4185==    at 0x4C2C171: operator delete(void*) (vg_replace_malloc.c:575)
==4185==    by 0x4A8663: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >::deallocate(std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2>*, unsigned long) (new_allocator.h:110)
==4185==    by 0x4A794E: std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> > >::deallocate(std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >&, std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2>*, unsigned long) (alloc_traits.h:377)
==4185==    by 0x4AA267: std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2>::_M_destroy() (shared_ptr_base.h:417)
==4185==    by 0x470E21: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:161)
==4185==    by 0x46AB12: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() (shared_ptr_base.h:546)
==4185==    by 0x4A1B03: std::__shared_ptr<md::sym::Registry, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() (shared_ptr_base.h:781)
==4185==    by 0x4A1B41: std::shared_ptr<md::sym::Registry>::~shared_ptr() (shared_ptr.h:93)
==4185==    by 0x78F4258: __run_exit_handlers (exit.c:82)
==4185==    by 0x78F42A4: exit (exit.c:104)
==4185==    by 0x78D9ECB: (below main) (libc-start.c:321)
==4185==  Block was alloc'd at
==4185==    at 0x4C2B145: operator new(unsigned long) (vg_replace_malloc.c:333)
==4185==    by 0x4A85AE: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >::allocate(unsigned long, void const*) (new_allocator.h:104)
==4185==    by 0x4A788F: std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> > >::allocate(std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >&, unsigned long) (alloc_traits.h:351)
==4185==    by 0x4A6A01: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<md::sym::Registry, std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, md::sym::Registry*, std::allocator<md::sym::Registry> const&) (shared_ptr_base.h:499)
==4185==    by 0x4A59FB: std::__shared_ptr<md::sym::Registry, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, std::allocator<md::sym::Registry> const&) (shared_ptr_base.h:957)
==4185==    by 0x4A44C9: std::shared_ptr<md::sym::Registry>::shared_ptr<std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, std::allocator<md::sym::Registry> const&) (shared_ptr.h:316)
==4185==    by 0x4A353D: std::shared_ptr<md::sym::Registry> std::allocate_shared<md::sym::Registry, std::allocator<md::sym::Registry>>(std::allocator<md::sym::Registry> const&) (shared_ptr.h:598)
==4185==    by 0x4A1FD8: std::shared_ptr<md::sym::Registry> std::make_shared<md::sym::Registry>() (shared_ptr.h:614)
==4185==    by 0x4A02E0: __static_initialization_and_destruction_0(int, int) (base.cpp:10)
==4185==    by 0x4A034B: _GLOBAL__sub_I_base.cpp (base.cpp:206)
==4185==    by 0x4B7BDC: __libc_csu_init (in /media/hdd/work/metadiff/metadiff-core/build/try)
==4185==    by 0x78D9E54: (below main) (libc-start.c:246)
==4185== 
==4185== Invalid read of size 4
==4185==    at 0x4559E5: __gnu_cxx::__exchange_and_add_single(int*, int) (atomicity.h:68)
==4185==    by 0x455AC5: __gnu_cxx::__exchange_and_add_dispatch(int*, int) (atomicity.h:84)
==4185==    by 0x470D6C: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:141)
==4185==    by 0x46AB12: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() (shared_ptr_base.h:546)
==4185==    by 0x4A1B03: std::__shared_ptr<md::sym::Registry, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() (shared_ptr_base.h:781)
==4185==    by 0x4A1B41: std::shared_ptr<md::sym::Registry>::~shared_ptr() (shared_ptr.h:93)
==4185==    by 0x78F45E9: __cxa_finalize (cxa_finalize.c:56)
==4185==    by 0x5B10E62: ??? (in /home/alex/.CLion2016.2/system/cmake/generated/metadiff-core-dac47eeb/dac47eeb/Debug/libmd_core.so)
==4185==    by 0x4010739: _dl_fini (dl-fini.c:252)
==4185==    by 0x78F4258: __run_exit_handlers (exit.c:82)
==4185==    by 0x78F42A4: exit (exit.c:104)
==4185==    by 0x78D9ECB: (below main) (libc-start.c:321)
==4185==  Address 0x7c95ab8 is 8 bytes inside a block of size 96 free'd
==4185==    at 0x4C2C171: operator delete(void*) (vg_replace_malloc.c:575)
==4185==    by 0x4A8663: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >::deallocate(std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2>*, unsigned long) (new_allocator.h:110)
==4185==    by 0x4A794E: std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> > >::deallocate(std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >&, std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2>*, unsigned long) (alloc_traits.h:377)
==4185==    by 0x4AA267: std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2>::_M_destroy() (shared_ptr_base.h:417)
==4185==    by 0x470E21: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:161)
==4185==    by 0x46AB12: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() (shared_ptr_base.h:546)
==4185==    by 0x4A1B03: std::__shared_ptr<md::sym::Registry, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() (shared_ptr_base.h:781)
==4185==    by 0x4A1B41: std::shared_ptr<md::sym::Registry>::~shared_ptr() (shared_ptr.h:93)
==4185==    by 0x78F4258: __run_exit_handlers (exit.c:82)
==4185==    by 0x78F42A4: exit (exit.c:104)
==4185==    by 0x78D9ECB: (below main) (libc-start.c:321)
==4185==  Block was alloc'd at
==4185==    at 0x4C2B145: operator new(unsigned long) (vg_replace_malloc.c:333)
==4185==    by 0x4A85AE: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >::allocate(unsigned long, void const*) (new_allocator.h:104)
==4185==    by 0x4A788F: std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> > >::allocate(std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >&, unsigned long) (alloc_traits.h:351)
==4185==    by 0x4A6A01: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<md::sym::Registry, std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, md::sym::Registry*, std::allocator<md::sym::Registry> const&) (shared_ptr_base.h:499)
==4185==    by 0x4A59FB: std::__shared_ptr<md::sym::Registry, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, std::allocator<md::sym::Registry> const&) (shared_ptr_base.h:957)
==4185==    by 0x4A44C9: std::shared_ptr<md::sym::Registry>::shared_ptr<std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, std::allocator<md::sym::Registry> const&) (shared_ptr.h:316)
==4185==    by 0x4A353D: std::shared_ptr<md::sym::Registry> std::allocate_shared<md::sym::Registry, std::allocator<md::sym::Registry>>(std::allocator<md::sym::Registry> const&) (shared_ptr.h:598)
==4185==    by 0x4A1FD8: std::shared_ptr<md::sym::Registry> std::make_shared<md::sym::Registry>() (shared_ptr.h:614)
==4185==    by 0x4A02E0: __static_initialization_and_destruction_0(int, int) (base.cpp:10)
==4185==    by 0x4A034B: _GLOBAL__sub_I_base.cpp (base.cpp:206)
==4185==    by 0x4B7BDC: __libc_csu_init (in /media/hdd/work/metadiff/metadiff-core/build/try)
==4185==    by 0x78D9E54: (below main) (libc-start.c:246)
==4185== 
==4185== Invalid write of size 4
==4185==    at 0x4559F0: __gnu_cxx::__exchange_and_add_single(int*, int) (atomicity.h:68)
==4185==    by 0x455AC5: __gnu_cxx::__exchange_and_add_dispatch(int*, int) (atomicity.h:84)
==4185==    by 0x470D6C: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:141)
==4185==    by 0x46AB12: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() (shared_ptr_base.h:546)
==4185==    by 0x4A1B03: std::__shared_ptr<md::sym::Registry, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() (shared_ptr_base.h:781)
==4185==    by 0x4A1B41: std::shared_ptr<md::sym::Registry>::~shared_ptr() (shared_ptr.h:93)
==4185==    by 0x78F45E9: __cxa_finalize (cxa_finalize.c:56)
==4185==    by 0x5B10E62: ??? (in /home/alex/.CLion2016.2/system/cmake/generated/metadiff-core-dac47eeb/dac47eeb/Debug/libmd_core.so)
==4185==    by 0x4010739: _dl_fini (dl-fini.c:252)
==4185==    by 0x78F4258: __run_exit_handlers (exit.c:82)
==4185==    by 0x78F42A4: exit (exit.c:104)
==4185==    by 0x78D9ECB: (below main) (libc-start.c:321)
==4185==  Address 0x7c95ab8 is 8 bytes inside a block of size 96 free'd
==4185==    at 0x4C2C171: operator delete(void*) (vg_replace_malloc.c:575)
==4185==    by 0x4A8663: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >::deallocate(std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2>*, unsigned long) (new_allocator.h:110)
==4185==    by 0x4A794E: std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> > >::deallocate(std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >&, std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2>*, unsigned long) (alloc_traits.h:377)
==4185==    by 0x4AA267: std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2>::_M_destroy() (shared_ptr_base.h:417)
==4185==    by 0x470E21: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:161)
==4185==    by 0x46AB12: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() (shared_ptr_base.h:546)
==4185==    by 0x4A1B03: std::__shared_ptr<md::sym::Registry, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() (shared_ptr_base.h:781)
==4185==    by 0x4A1B41: std::shared_ptr<md::sym::Registry>::~shared_ptr() (shared_ptr.h:93)
==4185==    by 0x78F4258: __run_exit_handlers (exit.c:82)
==4185==    by 0x78F42A4: exit (exit.c:104)
==4185==    by 0x78D9ECB: (below main) (libc-start.c:321)
==4185==  Block was alloc'd at
==4185==    at 0x4C2B145: operator new(unsigned long) (vg_replace_malloc.c:333)
==4185==    by 0x4A85AE: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >::allocate(unsigned long, void const*) (new_allocator.h:104)
==4185==    by 0x4A788F: std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> > >::allocate(std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >&, unsigned long) (alloc_traits.h:351)
==4185==    by 0x4A6A01: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<md::sym::Registry, std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, md::sym::Registry*, std::allocator<md::sym::Registry> const&) (shared_ptr_base.h:499)
==4185==    by 0x4A59FB: std::__shared_ptr<md::sym::Registry, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, std::allocator<md::sym::Registry> const&) (shared_ptr_base.h:957)
==4185==    by 0x4A44C9: std::shared_ptr<md::sym::Registry>::shared_ptr<std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, std::allocator<md::sym::Registry> const&) (shared_ptr.h:316)
==4185==    by 0x4A353D: std::shared_ptr<md::sym::Registry> std::allocate_shared<md::sym::Registry, std::allocator<md::sym::Registry>>(std::allocator<md::sym::Registry> const&) (shared_ptr.h:598)
==4185==    by 0x4A1FD8: std::shared_ptr<md::sym::Registry> std::make_shared<md::sym::Registry>() (shared_ptr.h:614)
==4185==    by 0x4A02E0: __static_initialization_and_destruction_0(int, int) (base.cpp:10)
==4185==    by 0x4A034B: _GLOBAL__sub_I_base.cpp (base.cpp:206)
==4185==    by 0x4B7BDC: __libc_csu_init (in /media/hdd/work/metadiff/metadiff-core/build/try)
==4185==    by 0x78D9E54: (below main) (libc-start.c:246)
==4185== 
==4185== 
==4185== HEAP SUMMARY:
==4185==     in use at exit: 96 bytes in 1 blocks
==4185==   total heap usage: 10,725 allocs, 10,724 frees, 688,288 bytes allocated
==4185== 
==4185== 96 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4185==    at 0x4C2B145: operator new(unsigned long) (vg_replace_malloc.c:333)
==4185==    by 0x4A85AE: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >::allocate(unsigned long, void const*) (new_allocator.h:104)
==4185==    by 0x4A788F: std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> > >::allocate(std::allocator<std::_Sp_counted_ptr_inplace<md::sym::Registry, std::allocator<md::sym::Registry>, (__gnu_cxx::_Lock_policy)2> >&, unsigned long) (alloc_traits.h:351)
==4185==    by 0x4A6A01: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<md::sym::Registry, std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, md::sym::Registry*, std::allocator<md::sym::Registry> const&) (shared_ptr_base.h:499)
==4185==    by 0x4A59FB: std::__shared_ptr<md::sym::Registry, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, std::allocator<md::sym::Registry> const&) (shared_ptr_base.h:957)
==4185==    by 0x4A44C9: std::shared_ptr<md::sym::Registry>::shared_ptr<std::allocator<md::sym::Registry>>(std::_Sp_make_shared_tag, std::allocator<md::sym::Registry> const&) (shared_ptr.h:316)
==4185==    by 0x4A353D: std::shared_ptr<md::sym::Registry> std::allocate_shared<md::sym::Registry, std::allocator<md::sym::Registry>>(std::allocator<md::sym::Registry> const&) (shared_ptr.h:598)
==4185==    by 0x4A1FD8: std::shared_ptr<md::sym::Registry> std::make_shared<md::sym::Registry>() (shared_ptr.h:614)
==4185==    by 0x5D28A1C: __static_initialization_and_destruction_0(int, int) (base.cpp:10)
==4185==    by 0x5D28A87: _GLOBAL__sub_I_base.cpp (base.cpp:206)
==4185==    by 0x4010139: call_init.part.0 (dl-init.c:78)
==4185==    by 0x4010222: call_init (dl-init.c:36)
==4185==    by 0x4010222: _dl_init (dl-init.c:126)
==4185== 
==4185== LEAK SUMMARY:
==4185==    definitely lost: 96 bytes in 1 blocks
==4185==    indirectly lost: 0 bytes in 0 blocks
==4185==      possibly lost: 0 bytes in 0 blocks
==4185==    still reachable: 0 bytes in 0 blocks
==4185==         suppressed: 0 bytes in 0 blocks
==4185== 
==4185== For counts of detected and suppressed errors, rerun with: -v

我初始化静态变量的方法是将它在头文件中声明为:

extern std::shared_ptr<Registry> const registry;

单个cpp文件包含:

std::shared_ptr<Registry> const registry = std::make_shared<Registry>();

Registry类也是如此:

class Registry:  public std::enable_shared_from_this<Registry>{
        public:
            /** The total number of ids already used */
            I total_ids;
            /** A register for all variables which are result of a floor operation */
            std::vector <std::pair<I, std::pair < Polynomial, Polynomial>>> floor_registry;
            /** A register for all variables which are result of a ceil operation */
            std::vector <std::pair<I, std::pair < Polynomial, Polynomial>>> ceil_registry;

            Registry(): total_ids(0){};
            /** Some functions */
        }

有关详细信息,这一切都构建在一个静态库中,我将根据我的程序构建它。是否有人只有关于可能导致此泄漏的指针或错误意味着什么的提示?

0 个答案:

没有答案