首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在链表上运行gtest时,在tcache2中检测到一个free():double free

在链表上运行gtest时,在tcache2中检测到一个free():double free
EN

Stack Overflow用户
提问于 2020-05-08 06:19:14
回答 1查看 49关注 0票数 0

我正在尝试完成我的类的作业,我可以找出错误的原因。如果有人能找到这个问题,我将不胜感激。当我试图找到错误时,所有的添加都会通过,似乎错误发生在测试的分辨率上。这是我掌握的最多信息了。

代码语言:javascript
复制
template<typename K, typename V>
LinkedListCollection<K,V>::LinkedListCollection() {
    head = nullptr;
    tail = nullptr;
    length = 0;
}

template<typename K, typename V>
LinkedListCollection<K,V>::LinkedListCollection(const LinkedListCollection<K,V>& rhs) {
    Node* cur = head;
    Node* iterator = rhs.head;
    cur = new Node;
    for(int i = 0; i < rhs.size(); i++) {
        cur->key = iterator->key;
        cur->value = iterator->value;
        cur->next = new Node;
        cur = cur->next;
    }
    cur->key = rhs.tail->key;
    cur->value = rhs.tail->value;
    tail = cur;
    cur = nullptr;
}

template<typename K, typename V>
LinkedListCollection<K,V>& LinkedListCollection<K,V>::operator=(const LinkedListCollection<K,V>& rhs) {
    make_empty();
    Node* cur = head;
    Node* rhs_cur = rhs.head;
    cur = new Node;
    for(int i = 0; i < rhs.size(); i++) {
        cur->key = rhs_cur->key;
        cur->value = rhs_cur->value;
        cur->next = new Node;
        cur = cur->next;
        rhs_cur = rhs_cur->next;
    }
    cur->key = rhs_cur->key;
    cur->value = rhs_cur->value;
    tail = cur;
}

template<typename K, typename V>
LinkedListCollection<K,V>::~LinkedListCollection() {
    make_empty();
}

template<typename K, typename V>
void LinkedListCollection<K,V>::add(const K& a_key, const V& a_val) {
    if(length == 0) {
        tail = new Node;
        tail->key = a_key;
        tail->value = a_val;
        tail->next = nullptr;
        head = tail;
        length++;
    }
    else {
        tail->next = new Node;
        tail->next->key = a_key;
        tail->next->value = a_val;
        tail->next->next = nullptr;
        tail = tail->next;
        length++;
    }
}

template<typename K, typename V>
void LinkedListCollection<K,V>::remove(const K& a_key) {
    Node* cur = head;
    if(head->key == a_key) {
        cur = head->next;
        delete head;
        head = cur;
        length--;
    }
    if(a_key == tail->key) {
        while(cur->next != tail)
            cur = cur->next;
        delete tail;
        tail = cur;
        length--;
    }
    else {
        if(cur->next != tail) {
            while(cur->next->key != a_key)
                cur = cur->next;
            Node* second = cur->next->next;
            delete cur->next;
            cur = second;
            length--;
        }
    }
}

template<typename K, typename V>
bool LinkedListCollection<K,V>::find(const K& search_key, V& the_val) const {
    Node* cur = head;
    for(int i = 0; i < length; i++) {
        if(cur->key == search_key) {
            the_val = cur->value;
            return true;
        }
    }
    return false;
}

template<typename K, typename V>
void LinkedListCollection<K,V>::find(const K& k1, const K& k2, std::vector<V>& vals) const {
    Node* cur = head;
    while(cur->key != k1)
        cur = cur->next;
    while(cur->key != k2) {
        vals.push_back(cur->value);
        cur = cur->next;
    }
    vals.push_back(cur->value);
}

template<typename K, typename V>
void LinkedListCollection<K,V>::keys(std::vector<K>& all_keys) const {
    Node* cur = head;
    for(int i = 0; i < length; i++) {
        all_keys.push_back(cur->key);
        cur = cur->next;
    }
}

template<typename K, typename V>
void LinkedListCollection<K,V>::sort(std::vector<K>& all_keys_sorted) const {
    Node* cur = head;
    for(int i = 0; i < length; i++) {
        all_keys_sorted.push_back(cur->key);
        cur = cur->next;
    }
    std::sort(all_keys_sorted.begin(), all_keys_sorted.end());
}

template<typename K, typename V>
int LinkedListCollection<K,V>::size() const {
    return length;
}

template<typename K, typename V>
void LinkedListCollection<K,V>::make_empty() {
    while(head) {
        Node* cur = head;
        while(cur->next) 
            cur = cur->next;
        delete cur;
    }
}


#endif

这是我运行的测试

代码语言:javascript
复制
TEST(BasicListTest, CorrectSize) {
  LinkedListCollection<string,double> c;
  ASSERT_EQ(0, c.size());
  c.add("b", 10.0);
  ASSERT_EQ(1, c.size());
  c.add("a", 20.0);
  ASSERT_EQ(2, c.size());
  c.add("c", 20.0);
  ASSERT_EQ(3, c.size());
}

我得到的信息是

free():在tcache2中检测到双重释放

EN

回答 1

Stack Overflow用户

发布于 2020-05-08 06:39:33

我打赌你的虫子在这里:

代码语言:javascript
复制
template<typename K, typename V>
void LinkedListCollection<K,V>::make_empty() {
    while(head) {
        Node* cur = head;
        while(cur->next) 
            cur = cur->next;
        delete cur;
    }
}

我认为它应该是这样的(但我没有测试它):

代码语言:javascript
复制
template<typename K, typename V>
void LinkedListCollection<K,V>::make_empty() {
    Node* cur = head;
    while(cur) {
        Node* tmp = cur;
        cur = cur->next;
        delete tmp;
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61668702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档