【问题标题】:Memory Leak when using SharedPtr with std::list! Bug?将 SharedPtr 与 std::list 一起使用时出现内存泄漏!漏洞?
【发布时间】:2017-03-16 01:18:47
【问题描述】:

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;
}

但是当使用 std::shared_ptr 或 boost::shared_ptr 时可以。 而且,如果我添加 'virtual ~A(){}',Poco::SharedPtr 也可以!

是 Poco::SharedPtr 的 bug 吗??

【问题讨论】:

  • Poco::SharedPtr 是否键入擦除删除器?否则,由于非虚拟基础析构函数,您的代码具有 UB。

标签: c++ memory-leaks poco stdlist


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 2013-11-02
    • 1970-01-01
    相关资源
    最近更新 更多