继承类内存泄漏
Inherited class memory leak
我在正在进行的项目中创建了这个模式:
struct Item {};
struct Base {
struct Result {
std::shared_ptr<Base> base_ptr;
Result(Base * base) : base_ptr(base) {}
};
virtual Base::Result update() const = 0;
};
struct Sub : public Base {
const std::vector<Item> items;
Sub(std::vector<Item> items) : items(items) {}
Base::Result update() const override {
return {new Sub{items}};
}
};
int main() {
std::vector<Item> items{Item{}};
auto base = std::shared_ptr<Base>{new Sub{items}};
auto result = base->update();
}
根据valgrind的说法,这会导致矢量中的泄漏:
==21663== 1 bytes in 1 blocks are definitely lost in loss record 1 of 2
==21663== at 0x4C29158: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==21663== by 0x401DC5: __gnu_cxx::new_allocator<Item>::allocate(unsigned long, void const*) (in /home/user/code/test_leak/a.out)
==21663== by 0x401C64: std::allocator_traits<std::allocator<Item> >::allocate(std::allocator<Item>&, unsigned long) (in /home/user/code/test_leak/a.out)
==21663== by 0x4019D7: std::_Vector_base<Item, std::allocator<Item> >::_M_allocate(unsigned long) (in /home/user/code/test_leak/a.out)
==21663== by 0x401890: std::_Vector_base<Item, std::allocator<Item> >::_M_create_storage(unsigned long) (in /home/user/code/test_leak/a.out)
==21663== by 0x401428: std::_Vector_base<Item, std::allocator<Item> >::_Vector_base(unsigned long, std::allocator<Item> const&) (in /home/user/code/test_leak/a.out)
==21663== by 0x401044: std::vector<Item, std::allocator<Item> >::vector(std::vector<Item, std::allocator<Item> > const&) (in /home/user/code/test_leak/a.out)
==21663== by 0x400EAA: Sub::Sub(std::vector<Item, std::allocator<Item> >) (in /home/user/code/test_leak/a.out)
==21663== by 0x400F4B: Sub::update() const (in /home/user/code/test_leak/a.out)
==21663== by 0x400D02: main (in /home/user/code/test_leak/a.out)
==21663==
==21663== LEAK SUMMARY:
==21663== definitely lost: 1 bytes in 1 blocks
如果代码被更改为返回shared_ptr而不是Base::Result,则不会发生泄漏,但我需要返回更多的shared_ptr才能将一些消息传递给接收器。
如果有一种传统的模式可以更好地解决这个问题,那么我当然对此感兴趣。否则,我想知道如何通过修改当前代码来修复泄漏。
您忘记在Base
结构中定义虚拟析构函数:
virtual ~Base() {}
相关文章:
- C++功能泄漏内存,我是C++新手,不确定如何解决
- 我的堆栈弹出式磁带的实现是否泄漏内存?
- 将 c++ 向量转换为字符 ** 而不会泄漏内存
- 析构函数 C++ 使泄漏内存
- 构造函数对象赋值是否泄漏内存
- corba :: orb_init泄漏内存
- Gmock泄漏内存
- 如何在不泄漏内存的情况下删除链接列表
- Visual C ODBC关闭记录集泄漏内存
- 为什么泄漏内存比在动态数组上执行 delete[] 慢
- OpenGL正在泄漏内存.哪个对象未释放
- 可以std ::退出泄漏内存
- uiautomation findall泄漏内存
- 为什么在此OpenCL代码中泄漏内存,为什么要泄漏内存
- pthread在完成后会泄漏内存
- win32 标准::线程泄漏内存
- 返回指向同一变量的指针是否会泄漏内存
- 使用clectType(new any_type())可能会泄漏内存泄漏
- Windows开发:如何确定我的应用程序是否正在泄漏内存
- WinHttp打开泄漏内存