为什么我的函数会造成内存泄漏? (C ++)

时间:2014-11-09 04:03:11

标签: 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 file

  #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

1 个答案:

答案 0 :(得分:0)

  

它将新节点插入链接列表。我是否需要删除列表中的newQuestion指针?

是。无论您使用new / new[]分配的资源是什么,都必须使用delete / delete[]将其删除。否则你会有内存泄漏。

  

不会弄乱我的名单吗?

delete / delete[]调用其操作数的析构函数,如果它是类类型的。如果你的Question类使用它的析构函数释放内部动态分配的内存,那么你就不会发生内存泄漏。

但你根本不必动态分配Question课程。只需将其声明为自动变量:

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

另请注意,这些评论指出了您尝试做的替代方案。