主程序 worker.cpp 包含:
unique_ptr<c_job> job;
...(loop)
job.reset(new c_xcol_job);
...(end loop)
...
c_xcol_job 类包含:
c_pack pack;
c_pack 类包含:
shared_ptr<c_imago> ref;
c_imago 类包含:
vector<shared_ptr<c_pixel>> pixel;
向量在 imago.h 中填充:
91 for(int i = 0; i < N; i++) {
92 shared_ptr<c_pixel_imago> px_buffer = make_shared<c_pixel_imago>();
93 px_buffer->v[0] = 0;
94 px_buffer->v[1] = 1;
95 pixel.push_back(px_buffer);
96 };
c_pixel 类包含:
class c_pixel {
public: double D;
...
并且类 c_pixel_imago 包含:
class c_pixel_imago : public c_pixel {
public: double v[2];
...
这似乎会导致内存泄漏。这是 valgrind 的输出:
==7252== 9,437,184 bytes in 3 blocks are possibly lost in loss record 543 of 544
==7252== at 0x4C2C7A7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7252== by 0x421905: __gnu_cxx::new_allocator<std::shared_ptr<c_pixel> >::allocate(unsigned long, void const*) (new_allocator.h:94)
==7252== by 0x420932: std::_Vector_base<std::shared_ptr<c_pixel>, std::allocator<std::shared_ptr<c_pixel> > >::_M_allocate(unsigned long) (in /worker.exe)
==7252== by 0x420A9D: void std::vector<std::shared_ptr<c_pixel>, std::allocator<std::shared_ptr<c_pixel> > >::_M_emplace_back_aux<std::shared_ptr<c_pixel> >(std::shared_ptr<c_pixel>&&) (vector.tcc:405)
==7252== by 0x41FA10: void std::vector<std::shared_ptr<c_pixel>, std::allocator<std::shared_ptr<c_pixel> > >::emplace_back<std::shared_ptr<c_pixel> >(std::shared_ptr<c_pixel>&&) (vector.tcc:102)
==7252== by 0x41E7F5: std::vector<std::shared_ptr<c_pixel>, std::allocator<std::shared_ptr<c_pixel> > >::push_back(std::shared_ptr<c_pixel>&&) (stl_vector.h:900)
==7252== by 0x412843: c_imago::import(std::string, std::string, double) (imago.h:95)
==7252== by 0x4120CC: c_imago::getd2(std::string) (imago.h:48)
==7252== by 0x41A2FE: c_xcol_job::launch() (xcol_job.h:42)
==7252== by 0x41B96D: main (worker.cpp:142)
==7252==
==7252== 46,094,112 bytes in 480,147 blocks are possibly lost in loss record 544 of 544
==7252== at 0x4C2C7A7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7252== by 0x422D7E: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<c_pixel_imago, std::allocator<c_pixel_imago>, (__gnu_cxx::_Lock_policy)2> >::allocate(unsigned long, void const*) (new_allocator.h:94)
==7252== by 0x42295F: std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<c_pixel_imago, std::allocator<c_pixel_imago>, (__gnu_cxx::_Lock_policy)2> > >::allocate(std::allocator<std::_Sp_counted_ptr_inplace<c_pixel_imago, std::allocator<c_pixel_imago>, (__gnu_cxx::_Lock_policy)2> >&, unsigned long) (alloc_traits.h:353)
==7252== by 0x422407: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<c_pixel_imago, std::allocator<c_pixel_imago>>(std::_Sp_make_shared_tag, c_pixel_imago*, std::allocator<c_pixel_imago> const&) (shared_ptr_base.h:522)
==7252== by 0x4219B5: std::__shared_ptr<c_pixel_imago, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator<c_pixel_imago>>(std::_Sp_make_shared_tag, std::allocator<c_pixel_imago> const&) (shared_ptr_base.h:997)
==7252== by 0x4209E9: std::shared_ptr<c_pixel_imago>::shared_ptr<std::allocator<c_pixel_imago>>(std::_Sp_make_shared_tag, std::allocator<c_pixel_imago> const&) (shared_ptr.h:317)
==7252== by 0x41F937: std::shared_ptr<c_pixel_imago> std::allocate_shared<c_pixel_imago, std::allocator<c_pixel_imago>>(std::allocator<c_pixel_imago> const&) (shared_ptr.h:599)
==7252== by 0x41E75B: _ZSt11make_sharedI12c_pixel_imagoIEESt10shared_ptrIT_EDpOT0_ (shared_ptr.h:615)
==7252== by 0x412665: c_imago::import(std::string, std::string, double) (imago.h:92)
==7252== by 0x4120CC: c_imago::getd2(std::string) (imago.h:48)
==7252== by 0x41A2FE: c_xcol_job::launch() (xcol_job.h:42)
==7252== by 0x41B96D: main (worker.cpp:142)
=
如何解决内存泄漏?