簡體   English   中英

雙端隊列指針內存泄漏

[英]Deque Pointer Memory Leak

我有一個使用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下運行此代碼(這只是一段代碼,項目很大),而Memory在默認構造函數內部的pVariantQueue = new VariantQueue處顯示內存泄漏為:

錯誤#46:泄漏8個直接字節+ 324個間接字節replace_operator_new d:\\ drmemory_package \\ common \\ alloc_replace.c(2899):std :: _ Allocate <> ??:0 std :: allocator <> :: allocate ??:0 std :: _ Wrap_alloc <> ::分配??:0 std :: _ Deque_alloc <> ::: _ Alloc_proxy ??:0 std :: _ Deque_alloc <> ::: _ Deque_alloc <> ??:0 std :: deque <> :: deque <> ??: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);

維奈

您的內存泄漏很可能在析構函數中找到。 隊列消失后,隊列中的對象會如何處理?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM