0

C++を学習中にLinkedListを作成してみたのですが、メモリリークが起きてしまい、解決方法がわかりません。いろいろ試してはみたのですが、どうしてもわからないのでご教授いただければ幸いです。

追記:
信用度が足りずコメントができないのでこちらから失礼いたします。ご回答ありがとうございます。メモリリークしているのかもと思ったのはログで普段は出てこなかった The thread 0x6734 has exited with code -1073741510 (0xc000013a).が出てきたためなのですが、いま改めて確認してみたら、メモリリークがある場合はDetected memory leaks!と出るのですね。まったくの勘違いでした。ありがとうございました。Insertの実装についても、調べてみます。

// LinkedList.cpp : Defines the entry point for the console application.
//
#define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h>  
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class Node {
    int data;
    class Node* next;

public:
    Node(int param) {
        data = param;
        next = nullptr;
    }

    ~Node() {
        if (next != nullptr)
            next = nullptr;
    }

    void SetData(int param) {
        this->data = param;
    }

    int GetData() {
        return this->data;
    }


    void SetNext(Node* param) {
        this->next = param;
    }

    Node* GetNext() {
        return this->next;
    }

};

class LinkedList {

private:

    Node* head = nullptr;
    Node* tail = nullptr;

public:
    LinkedList() {}
    ~LinkedList() {
        Node* node = head;
        while (node != nullptr) {
            Node* next = node->GetNext();
            delete node;
            node = next;
        }
    }

    void Add(int value) {
        if (head == nullptr) {
            head = new Node(value);
            tail = head;
        }
        else {
            Node* next = new Node(value);
            tail->SetNext(next);
            tail = next;
        }
    }

    bool Insert(int value, int index) {
        Node* node = head;
        if (index == 0) {
            Node* target = new Node(value);
            target->SetNext(head);
            head = target;
            return true;
        }

        for (int i = 0; i < index-1; i++) {
            if (node == nullptr) {
                return false;
            }

            Node* next = node->GetNext();

            if (next == nullptr) {
                break;
            }

            node = node->GetNext();
        }

        Node* target = new Node(value);
        target->SetNext(node->GetNext());
        node->SetNext(target);
        return true;
    }

    void Print() {
        Node* node = head;
        while (node != nullptr) {
            cout << node->GetData() << endl;
            node = node->GetNext();
        }
    }
};


int main()
{
    LinkedList* li = new LinkedList();
    li->Add(1);
    li->Add(2);
    li->Add(3);
    li->Insert(0, 0);
    li->Insert(7, 2);

    li->Print();

    delete li;

    _CrtDumpMemoryLeaks();
    getchar();
    return 0;
}
3
  • 3
    Insertの実装など気になる点はありますが)質問のメモリリークについては特に見当たりませんし、実行してみても何も検出されませんでした。質問者さんは何をもってメモリリークと見なしたのか、その辺りをきちんと質問文に記載していただけたらと思います。
    – sayuri
    2018年3月1日 1:32
  • 誤検知の可能性は検討してみましたでしょうか。つまりLinkedList関連行を全て取り除いた場合と検出結果を比べるわけですけと。
    – Uncle-Kei
    2018年3月1日 1:37
  • 2
    この編集は質問者さんによるものでしょうか? 意図せずアカウントが分かれてしまった場合は「間違えてアカウントを 2 件作成してしまいました。どうしたらマージできますか?」をご覧下さい。
    – mjy
    2018年3月1日 9:34

1 件の回答 1

3

メモリリークしているのかもと思ったのはログで普段は出てこなかった The thread 0x6734 has exited with code -1073741510 (0xc000013a).が出てきたため

0xc000013aはSTATUS_CONTROL_C_EXIT

{Application Exit by CTRL+C} The application terminated as a result of a CTRL+C.

という意味だそうです。心当たりはないでしょうか?

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。