2016-07-14 14 views
-1

MAIN.CPPここでメモリリークが発生するのはなぜですか?メイン関数で

#include<iostream> 
#include "Queue.h" 

using namespace std; 
char* PotionTypeString(PotionType type) 
{ 
    char* s = "UNKNOWN"; 

    switch (type) { 
    case SPEED: 
     s = "Speed"; 
     break; 
    case STRENGTH: 
     s = "Strength"; 
     break; 
    case HEALTH: 
     s = "Health"; 
     break; 
    case WISDOM: 
     s = "Wisdom"; 
     break; 
    } 
    return(s); 
} 

int main() 
{ 
    Queue q; 
    q.enqueue(WISDOM); 
    q.dequeue(); 
    #ifdef _WIN32 
    if (_CrtDumpMemoryLeaks()) { 
     cout << "Memory leaks!" << endl; 
    } else { 
     cout << "No leaks" << endl; 
    } 
    #endif 
    return 0; 
} 

Queue.cpp

#include "Queue.h" 
#include <string.h> 
#include <iostream> 

using namespace std; 

Queue::Queue() { 
    front = NULL; 
    rear = NULL; 
    size = 0; 
} 

Queue::~Queue() { 
    Node* cur = front; 

    while (cur != NULL) { 
     Node* temp = cur->next; 
     delete cur; 
     cur = temp; 
    } 
} 

void Queue::enqueue(PotionType type) { 
    Node* node = new Node(); 
    node->type = type; 

    if (front == NULL) { 
     front = node; 
    } 
    else { 
     rear->next = node; 
    } 
    rear = node; 
    size = size + 1; 
} 

PotionType Queue::dequeue() { 
    PotionType toRet; 
    if (front != NULL) { 
     Node* node = new Node(); 

     node = front; 
     front = front->next; 

     toRet = node->type; 
     delete(node); 
     size = size - 1; 
    } 
    return toRet; 
} 

void Queue::print() { 
    if (front == NULL) { 
     cout << "Empty list" << endl; 
    } 
    else { 
     Node * toPrint = new Node(); 
     toPrint = front; 

     while (toPrint != NULL) { 
      cout << PotionTypeString(toPrint->type) << endl; 
      toPrint = toPrint->next; 
     } 
    } 
} 

私はちょうど空のキューのインスタンスを、単一の項目を追加し、その後、単一のアイテムをデキューメモリリークが発生すると、デキューメソッドまたはデストラクタと関連があると感じています。

私はC++の新機能ですから、私はそうではありません完全に確かです。

ここで私を助けたい人はいますか?

編集:

は私がuser4581301によって提案された変更に入れ、そしてそれは私がq.dequeue()を削除し、それを残す場合は、単純に、しかし

Queue q; 
q.enqueue(WISDOM); 
q.dequeue(); 

を行く私のメモリリークの問題を修正デストラクタまで、私はメモリリークを受け取ります。 Queue::dequeue

Node* node = new Node(); 

答えて

1

はすぐfrontnodeを指すように

Node* node = front; 

で両方のラインを置き換えるアドレスだ新しいノードが速やかに上書きされて

node = front; 

により漏洩している必要があります割り当て十分である。

Miles Budnekが指摘しているように、同じエラーはQueue::printです。あなたが絶対必要な場合を除き、newを使わないでください。newのすべてに対応するdeleteが必要です。

+0

同じことが 'Queue :: print'で起こります。 –

+0

@ user4581301 OPを編集しました。 –

+0

@DylanHolmes 'Queue q;'は 'main'関数が終了するまで破壊されない静的' Queue'を割り当てます。これは、リークチェッカーの呼び出し後です。代わりに 'q'の範囲を減らしてリークチェッカーを呼び出すと、あなたのリークがなくなるか、デストラクタにバグがあります。これを試してください: '{Queue q; q.enqueue(WISDOM);} '' q''は、中括弧とリークチェッカーの前に破棄されます。私は、おそらく次の、つまりまだ隠されていないものとして答えるだろうから[What is The Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)質問。 – user4581301

0

ポインタを使用した後、ポインタを削除してからNULLに設定します。例:これが動作するために、それはスコープの外に出ないように

もちろん
char* PotionTypeString(PotionType type) 
{ 
    char* s = "UNKNOWN"; 

    switch (type) { 
    } 
return(s); 
} 

int main() 
{ 
    Queue q; 
    q.enqueue(WISDOM); 
    q.dequeue(); 
    delete s; 
    s = NULL; 
    #ifdef _WIN32 
    #endif 
    return 0; 
} 

sPotionTypeString()外で宣言する必要があります。

関連する問題