2016-04-19 26 views
0

私はSTDを使用している中でのstruct ::両端キューの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; 
} 

私は博士のメモリの下でこのコードを実行していますがデフォルトコンストラクタ 内部 pVariantQueue = new VariantQueueでのメモリリークを示すコードの一部、プロジェクトはかなり大きいです)博士メモリーなど:

エラー#46:LEAK 8ダイレクトバイト+ 324バイト間接 replace_operator_new D:\ drmemory_package \共通\のalloc_replace.c(2899): のstd :: _ ??> <を割り当て:0 のstd ::アロケータ<> :: ?? を割り当てる:0 のstd :: _ Wrap_alloc <は> :: ??割り当てる:0 STDを:: _ Deque_alloc <> :: _ Alloc_proxy ??:0 のstd :: _ Deque_alloc <> :: _ Deque_alloc <> ??:0 std :: deque < :: :: deque <> ??:0 AttributeValueWrapper :: AttributeValueWrapper

あなたにこの問題に関する考えを共有してください。

Iも 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);

でメモリリークビナイ

+0

デキューをメンバー変数にしない理由は何ですか? std :: swap()を使ってコンテンツを効率的に移動することができます –

+0

コードをもっとシェアしてください。 –

+1

'pVariantQueue = std :: make_unique (new VariantQueue);' 'pVariantQueue = std :: make_unique ();'をしたくないのですか?おそらく新しい 'VariantQueue'を作成し、' make_unique'でコピーコンストラクタを呼び出しますか?したがって、newによって割り当てられたメモリがリークします。 –

答えて

1

を得ます。キューが消えたときにキュー内のオブジェクトはどうなりますか?

関連する問題