首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >共享指针和内存泄漏的C++11向量

共享指针和内存泄漏的C++11向量
EN

Stack Overflow用户
提问于 2013-06-24 19:34:11
回答 2查看 1.3K关注 0票数 2

主程序worker.cpp包含:

代码语言:javascript
复制
    unique_ptr<c_job> job;
    ...(loop)
        job.reset(new c_xcol_job);
    ...(end loop)
    ...

c_xcol_job类包含:

代码语言:javascript
复制
    c_pack pack;

c_pack类包含:

代码语言:javascript
复制
shared_ptr<c_imago> ref;

c_imago类包含:

代码语言:javascript
复制
vector<shared_ptr<c_pixel>> pixel;

向量在imago.h中填充为:

代码语言:javascript
复制
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类包含:

代码语言:javascript
复制
class c_pixel {
    public: double D;
...

c_pixel_imago类包含:

代码语言:javascript
复制
class c_pixel_imago : public c_pixel {
    public: double v[2];
... 

这似乎会导致内存泄漏。下面是valgrind的输出:

代码语言:javascript
复制
==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)
=

如何解决内存泄漏问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-24 22:29:56

代码语言:javascript
复制
   unique_ptr<c_job> job;
...(loop)
    job.reset(new c_xcol_job);
...(end loop)
...

你是否声明了c_job的析构函数为virtual?如果不是,它将不会调用c_xcol_job的析构函数。

票数 2
EN

Stack Overflow用户

发布于 2013-06-24 19:40:09

如果成员v是动态分配的数组,并且您没有在c_pixel_imago的析构函数中对其进行delete [],则可能会发生内存泄漏

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17274484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档