C++ 继承类内存泄漏
我在正在进行的项目中创建了此模式:C++ 继承类内存泄漏,c++,memory-leaks,C++,Memory Leaks,我在正在进行的项目中创建了此模式: 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 { co
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();
}
struct Item{};
结构基{
结构结果{
std::共享的ptr基ptr;
结果(Base*Base):Base_ptr(Base){
};
虚拟基::结果更新()常量=0;
};
结构子:公共基础{
常量std::向量项;
子(标准::向量项):项(项){}
Base::Result update()常量重写{
返回{new Sub{items}};
}
};
int main(){
std::向量项{Item{};
auto base=std::shared_ptr{new Sub{items};
自动结果=基本->更新();
}
根据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
==21663==1个块中的1个字节肯定会在丢失记录(共2个)中丢失
==21663==at 0x4C29158:新运算符(无符号长)(在/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so中)
==21663==by 0x401DC5:u gnu_cxx::新的_分配器::分配(无符号长,无效常量*)(in/home/user/code/test_leak/a.out)
==21663==by 0x401C64:std::allocator\u traits::allocate(std::allocator&,无符号长)(in/home/user/code/test\u leak/a.out)
==21663==by 0x4019D7:std::_Vector_base::_M_allocate(无符号长)(in/home/user/code/test_leak/a.out)
==21663==by 0x401890:std::_Vector_base::_M_create_storage(无符号长存储)(in/home/user/code/test_leak/a.out)
==21663==by 0x401428:std::_Vector_base::_Vector_base(无符号长,std::分配器常量&)(in/home/user/code/test_leak/a.out)
==21663==by 0x401044:std::vector::vector(std::vector const&)(in/home/user/code/test_leak/a.out)
==21663==0x400EAA:Sub::Sub(std::vector)(in/home/user/code/test_leak/a.out)
==21663==by 0x400F4B:Sub::update()常量(in/home/user/code/test_leak/a.out)
==21663==by 0x400D02:main(in/home/user/code/test\u leak/a.out)
==21663==
==21663==泄漏汇总:
==21663==肯定丢失:1个块中有1个字节
如果将代码更改为返回共享的_ptr而不是Base::Result,则不会出现泄漏,但我需要返回比共享的_ptr更多的内容,以便将一些消息传递给接收方
如果有一种传统的模式可以更好地解决这个问题,那么我当然对此感兴趣。否则,我想知道如何通过修改当前代码来修复泄漏。您忘记了在
Base
struct:
virtual ~Base() {}
您忘记了在
Base
struct:
virtual ~Base() {}
如果将
base_ptr
的类型从shared_ptr
更改为unique_ptr
,则应该能够避免泄漏。如果将base_ptr
的类型从shared_ptr
更改为unique_ptr
,则应该能够避免泄漏。