Deque指针内存泄漏

Deque Pointer Memory Leak

本文关键字:泄漏 内存 指针 Deque      更新时间:2023-10-16

我有一个结构,在其中我使用了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个间接字节替换操作员新d: \drmemory_package\common\alloc_replace.c(2899):std::_Allocate<>:0std::分配器<>::分配:0std::_Wrap_alloc<>::分配:0std::_Deque_alloc<>::_分配代理(_P):0std::_Deque_alloc<>::_Deque_alloc<>:0std::deque<>::deque<>:0AttributeValueWrapper::AttributeValueWraper

请分享你对这个问题的看法。

我也尝试过使用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);

Vinay

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