Deque Pointer Memory Leak

时间:2016-04-19 14:52:14

标签: c++ memory-leaks

我有一个结构,其中我使用了std :: deque

class VariantWrapper;
typedef _STL_NAMESPACE_::deque<VariantWrapper> VariantQueue;

struct AttributeValueWrapper
{
AttributeValueWrapper() : bAttributeIsArray(false)
{
    pVariantQueue = new VariantQueue;
    if(!pVariantQueue)
        throw std::bad_alloc();
}
AttributeValueWrapper(const AttributeValueWrapper& a)
{
    pVariantQueue = a.TakeOwner();
    bAttributeIsArray = a.bAttributeIsArray;
}
AttributeValueWrapper& operator=(AttributeValueWrapper& r)
{
    throw std::bad_exception("equal operator not supported in AttributeValueWrapper");
}
VariantQueue* TakeOwner() const
{
    VariantQueue *p = pVariantQueue;
    pVariantQueue = NULL;
    return p;
}
~AttributeValueWrapper()
{
    if (pVariantQueue)
    {
        delete pVariantQueue;
    }

}

bool bAttributeIsArray;
mutable VariantQueue *pVariantQueue;};

主要方法:

int main()
{
 AttributeValueWrapper attrib;
}

我在Dr Memory下运行这段代码(这只是一段代码,项目非常大)而且Dr Memory显示内存泄漏 pVariantQueue = new VariantQueue在默认构造函数中 为:

  

错误#46:LEAK 8个直接字节+ 324个间接字节   replace_operator_new       d:\ drmemory_package \共同\ alloc_replace.c(2899):   的std :: _分配&LT;&GT;        - :0   的std ::分配器&LT;&GT; ::分配        - :0   的std :: _ Wrap_alloc&LT;&GT; ::分配        - :0   的std :: _ Deque_alloc&LT;&GT; :: _ Alloc_proxy        - :0   的std :: _ Deque_alloc&LT;&GT; :: _ Deque_alloc&LT;&GT;        - :0   的std ::双端队列&LT;&GT; ::双端队列&LT;&GT;        - :0   AttributeValueWrapper :: AttributeValueWrapper

请分享您对此问题的看法。

我也尝试过使用std::unique_ptr,但仍然在同一行没有(同一点)获得相同的内存泄漏:

struct AttributeValueWrapper
{
AttributeValueWrapper() : bAttributeIsArray(false)
{
    pVariantQueue = std::make_unique<VariantQueue>(new VariantQueue);
    if(!pVariantQueue)
        throw std::bad_alloc();
}
AttributeValueWrapper(const AttributeValueWrapper& a)
{
    pVariantQueue = a.TakeOwner();
    bAttributeIsArray = a.bAttributeIsArray;
}
AttributeValueWrapper& operator=(AttributeValueWrapper& r)
{
    throw std::bad_exception("equal operator not supported in AttributeValueWrapper");
}
std::unique_ptr<VariantQueue> TakeOwner() const
{
    std::unique_ptr<VariantQueue> p = std::move(pVariantQueue);
    pVariantQueue = NULL;
    return p;
}

~AttributeValueWrapper()
{

}

bool bAttributeIsArray;
mutable std::unique_ptr<VariantQueue> pVariantQueue;

};

现在出现内存泄漏

pVariantQueue = std::make_unique<VariantQueue>(new VariantQueue);

维奈

1 个答案:

答案 0 :(得分:1)

您的内存泄漏最有可能在析构函数中找到。当队列消失时,队列中的对象会发生什么?

相关问题