2014-11-09 116 views
0

我在下面的程序中創建一個內存泄漏的代碼行,我不知道爲什麼是這種情況...爲什麼我的函數產生內存泄漏? (C++)

泄漏正在創建的代碼行。

Question* newQuestion = new Question(text, mark, answers, numAnswers, &this->operator[](qNum-1)); 

它將新節點插入到鏈接列表中。在列表中是否需要刪除'newQuestion'指針?這不會弄亂我的名單嗎?

當我通過調試器運行它時,列表的其餘部分看起來像是通過析構函數銷燬的。我只是不明白爲什麼這個參考文獻不會消失。

不確定是否應該發佈剩餘的代碼或不需要,因爲它有點冗長......我希望這可能只是一個明顯的東西,我作爲一個C++新手缺少。

其全部的方法是:

bool Exam::ReplaceDeleteQuestion(){ 
int qNum; 
char repDel; 
Question* temp = phead; 

cout << "Which question would you like to modify? Please enter the number (1, 2, ...)" <<endl; 
cin >> qNum; 

for(int i = 0; i<(qNum - 1); i++){ 
    if(temp == NULL || temp->GetNext() == NULL){ 
     cout << "Please enter an element within the bounds of the linked list"<<endl; 
     return true; 
    } 
    temp = temp->GetNext(); 
} 

cout << "Do you want to Replace or Delete? (R = Replace, D = Delete): "; 
cin >> repDel; 

while(repDel != 'R' && repDel != 'r' && repDel != 'D' && repDel != 'd'){ 
    cout << "Try again: R = Replace, D = Delete: "; 
    cin >> repDel; 
} 

//Set up new question and replace 
if(repDel == 'r' || repDel == 'R'){ 

    char* questionBuffer = new char[200]; 
    cout << "Please enter the new question text below"<<endl; 
    cin.ignore(200, '\n'); //Ignore newline character 
    cin.getline(questionBuffer, 200); 
    char* text = new char[strlen(questionBuffer) + 1]; 
    strcpy_s(text, strlen(questionBuffer)+1, questionBuffer); //Copy questionBuffer into text field 
    delete[] questionBuffer; //free question buffer 

    cout << "Please enter the new question mark: "<<endl; 
    int mark; 
    cin >> mark; 
    cout << "How many answers are there now? : "; 
    int numAnswers; 
    cin >> numAnswers; 

    Answer **answers = new Answer*[numAnswers]; //Allocate memory for the answer member 

    for(int i = 0; i < numAnswers; i++){ 
     cout << "Please enter answer " << i+1 <<endl; //Prompt user for text 
     answers[i] = new Answer(); //Create the answer 
    } 

    Question* newQuestion = new Question(text, mark, answers, numAnswers, &this->operator[](qNum-1)); 

    this->operator[](qNum-1) = *newQuestion; 

    delete[] answers; 
    //delete[] text; 


    return true; 
} 

.h文件

#ifndef QUESTION_H_ 
    #define QUESTION_H_ 

    #include <iostream> 

    // Question.h 
    class Question 
{ 
    char* text; 
    unsigned int mark; 
    Answer** answers; 
    unsigned int numAnswers; 
    Question* pNext; 
public: 
    Question():text(0),mark(0),answers(0),numAnswers(0),pNext(0){}; 
    Question(char*, unsigned int, Answer**, unsigned int, Question*); 
    Question(Question&); 
    ~Question(); 

    Question*& GetNext() 
    { 
     return pNext; 
    } 
    Answer& operator[](unsigned int i);   //overloaded indexing 
    Question& operator=(Question&);    // overloaded assignment 
    friend ostream& operator<<(ostream&, Question&); // overloaded insertion 
}; 

#endif 
+0

您在堆上分配一個對象(原因不清)。然後將該對象複製到'this-> operator [](qNum-1)'返回的任何內容中。原始對象從不再使用,特別是永遠不會被釋放。 – 2014-11-09 04:05:23

+1

使用'std :: string'和'std :: vector',內存泄漏將神奇地消失。我沒有仔細查看你的代碼,因爲代碼太多與問題無關。 – 2014-11-09 04:05:28

+1

「問題」類應該與任何數據結構無關。如果你想在堆棧中存儲一些問題,一些在地圖中?而是使用'std :: list '。沒有'新'需要更多。 – 2014-11-09 04:07:04

回答

0

它插入新節點進入一個鏈表。它在列表中後是否需要刪除newQuestion指針?

是的。無論您分配的資源是new/new[],您需要必須delete/delete[]將其刪除。否則,你會有內存泄漏。

這不會搞亂我的名單嗎?

delete/delete[]如果它是類類型,則調用其操作數的析構函數。如果Question類解除分配它在內部動態分配的內存使用它的析構函數,那麼你不會得到內存泄漏。

但是你不應該動態分配一個Question類。只需將它聲明爲自動變量:

Question newQuestion(text, mark, answers, numAnswers, &this->operator[](qNum-1)); 

還要注意,那點破替代你想要做什麼評論。

+0

我很關心什麼問題構造函數用它傳遞的指針做什麼。 – 2014-11-09 04:09:42

+0

@NeilKirk確實,這看起來很奇怪。 – 0x499602D2 2014-11-09 04:10:49