共享指针和内存泄漏的C++11向量
C++11 vector of shared pointers and memory leaks
主程序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];
...
这似乎会导致内存泄漏。这是瓦尔格林德的输出:
==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)
=
如何解决内存泄漏?
unique_ptr<c_job> job;
...(loop)
job.reset(new c_xcol_job);
...(end loop)
...
您是否声明c_job的析构函数是虚拟的?否则,它不会调用c_xcol_job的析构函数。
如果成员v
是一个动态分配的数组,并且您没有在c_pixel_imago
析构函数中delete []
它,则可能会得到内存泄漏
相关文章:
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- C++11 迭代向量的新方法?
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- 在 C++11 及更高版本中,有没有办法初始化初始值设定项列表中的向量?
- C++11,仅将一个字段复制到向量中
- C++11-在结构向量中修改结构的成员
- 分割错误:使用向量时为 11 c++
- C++11 向量包含 2 个不同的子类,但不能同时进行
- 标准向量之上的 C++11 包装类
- 如何使用 c++11 语法创建 for 循环以遍历向量
- 如何在 C++11 中启动具有相同值的向量的向量
- C ++ 11,为什么为向量定义unordered_map这个不起作用<int>?
- 将元素从C 11线程安全地添加到向量中
- 使用GCC v4.8在Ubuntu 14.04上定义C 11中元组向量时的编译错误
- 在 C++11 中计算字母和单词的双字母组合的 std::线程向量的问题
- 如何在C++中对向量进行排序和排名(不使用 C++11)
- 尝试加载DirectX 11中的obj文件时,向量下标
- 有没有办法<int><double>在使用 C++11 函数调用期间自动将"向量"提升为"向量"?
- 共享指针和内存泄漏的C++11向量
- 两个glfloat为对的c++ 11向量初始化失败