用于c ++命令行应用程序的Xcode内存泄漏检测

时间:2017-08-14 20:08:21

标签: c++ xcode memory-leaks

我试图使用泄漏工具来检测我实施的链接列表中的内存泄漏。

这是代码 -

#ifndef LinkedList_h
#define LinkedList_h

#include <initializer_list>
#include <iostream>
#include <stdexcept>

using std::runtime_error;
using std::initializer_list;
using std::ostream;

template <typename T>
class LinkedList {
    template <typename U>
    friend ostream& operator<<(ostream &, const LinkedList<U> &);
public:

    typedef T value_type;
    typedef int size_type;

private:

    struct Node {
        T value;
        Node *next;
        Node *prev;
    };

    Node *head;
    Node *tail;

    size_type n_elems;

public:

    LinkedList();
    LinkedList(std::initializer_list<T> il);
    ~LinkedList();

    size_type size() const;

    bool empty() const;

    void push_front(const T&);
    void push_back(const T&);
    void pop_back();
    void pop_front();

    T& front() const;
    T& back() const;
};

template <typename T>
ostream& operator<<(ostream&, const LinkedList<T> &);

template <typename T>
LinkedList<T>::LinkedList() : head(nullptr), tail(nullptr), n_elems(0) { }

template <typename T>
LinkedList<T>::LinkedList(std::initializer_list<T> il) : head(nullptr), tail(nullptr), n_elems(0) {
    // insert all the elements from the list here
    // re-use the insert operation after implementing
    for(auto &elem: il) {
        push_back(elem);
    }
}

template <typename T>
T& LinkedList<T>::back() const {
    if(tail == nullptr)
        throw runtime_error("back() on empty LinkedList!\n");

    return tail->value;
}

template <typename T>
T& LinkedList<T>::front() const {
    if(head == nullptr)
        throw runtime_error("front() on empty LinkedList!\n");

    return head->value;
}

template <typename T>
typename LinkedList<T>::size_type LinkedList<T>::size() const {
    return n_elems;
}

template <typename T>
bool LinkedList<T>::empty() const {
    return n_elems == 0;
}

template <typename T>
void LinkedList<T>::push_front(const T& elem) {
    Node *new_node = new Node;
    new_node->value = elem;
    new_node->next = nullptr;
    new_node->prev = nullptr;

    if(head == nullptr) {
        head = tail = new_node;
        ++n_elems;
        return;
    }

    new_node->next = head;
    head->prev = new_node;
    head = new_node;

    ++n_elems;
}

template <typename T>
void LinkedList<T>::push_back(const T& elem) {
    Node *new_node = new Node;
    new_node->value = elem;
    new_node->next = nullptr;
    new_node->prev = nullptr;

    if(tail == nullptr) {
        head = tail = new_node;
        ++n_elems;
        return;
    }

    new_node->prev = tail;
    tail->next = new_node;
    tail = new_node;

    ++n_elems;
}

template <typename T>
void LinkedList<T>::pop_back() {
    if(tail == nullptr)
        throw runtime_error("pop_back() on empty LinkedList\n");

    if(head == tail) {
        delete head;
        head = tail = nullptr;
        --n_elems;
        return;
    }

    Node *to_remove = tail;
    to_remove->prev->next = nullptr;
    tail = to_remove->prev;

    delete to_remove;

    --n_elems;
}

template <typename T>
void LinkedList<T>::pop_front() {
    if(head == nullptr)
        throw runtime_error("pop_front() on empty LinkedList\n");

    if(head == tail) {
        delete head;
        head = tail = nullptr;
        --n_elems;
        return;
    }

    Node *to_remove = head;
    to_remove->next->prev = nullptr;
    head = to_remove->next;

    delete to_remove;

    --n_elems;
}

template <typename T>
ostream& operator<<(ostream &out, const LinkedList<T> &list) {
    typename LinkedList<T>::Node *crawler = list.head;

    while(crawler != nullptr) {
        out << crawler->value << " ";
        crawler = crawler->next;
    }

    return out;
}

template <typename T>
LinkedList<T>::~LinkedList(){
    typename LinkedList<T>::Node *crawler = head;

    while(crawler != nullptr) {
        auto *next = crawler->next;
        delete crawler;
        crawler = next;
    }
}


#endif /* LinkedList_h */

这是驱动程序 -

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

using std::cout;
using std::cin;
using std::endl;

int main(void) {
    LinkedList<int> int_list;

    for(int i = 0; i < 10; ++i) {
        int_list.push_back(i);
    }

    cout << int_list.size() << endl;
    cout << int_list << endl;

    for(int i = 0; i < 8; ++i)
        int_list.pop_front();

    cout << int_list << endl;
    cout << int_list.size() << endl;

    int_list.pop_back();

    cout << int_list << endl;

    int *myInt = new int;
    *myInt = 0;

    myInt = nullptr;

    return 0;
}

现在,当我尝试执行Product-&gt; Profile-&gt; Leaks然后按下录制按钮时,没有任何反应。计时器保持在00。

这是一张照片 -

Screenshot of profiler. 有人可以告诉我这里出了什么问题吗?我错过了一些步骤吗?

我故意在驱动程序结束时添加泄漏仍然似乎没有发生。需要帮助。

0 个答案:

没有答案