gpt4 book ai didi

c++ - 将 SharedPtr 与 std::list 一起使用时出现内存泄漏!漏洞?

转载 作者:行者123 更新时间:2023-11-27 23:55:34 25 4
gpt4 key购买 nike

VLD 在以下代码中检测到内存泄漏:

typedef Poco::SharedPtr<double> DoublePtr;
class A {
public:
DoublePtr a;
};

class B:public A {
public:
DoublePtr b;
};

class C : public B
{
public:
DoublePtr c;

};

typedef Poco::SharedPtr<A> APtr;
typedef Poco::SharedPtr<B> BPtr;
typedef Poco::SharedPtr<C> CPtr;

class Test {
public:
Test() {
CPtr c1 = new C();
a_list.push_back(c1);
}

std::list<APtr> a_list;
};

int main(int argc, char *argv[])
{

Test test;
}

But it's OK when using std::shared_ptr or boost::shared_ptr instead. And, if I add 'virtual ~A(){}', Poco::SharedPtr is OK too!

Is the Bug of Poco::SharedPtr??

最佳答案

A 需要有一个虚析构函数;如果不是,则通过 A 指针删除 BC 对象会导致未定义的行为。未定义的行为可能包括内存泄漏。

关于c++ - 将 SharedPtr 与 std::list 一起使用时出现内存泄漏!漏洞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42823643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com