链表与内存泄漏问题

时间:2019-04-24 03:27:20

标签: c++ oop linked-list

该程序充当具有唯一名称和颜色的兔子对象链接列表的模板。

int Nodes::insert()
{

  Node* new_node = (Node*) malloc(sizeof(Node));

  new_node->name = getName();

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

  if (head != NULL)
  {
    head->prev = new_node;
  }

  head = new_node;

  return 0;
}

当节点(兔子)插入到链表中时,该属性是在创建节点(兔子)时从返回的函数调用中分配的。

new_node->name = getName();

在我调用它以创建多个节点导致此错误之前,该函数调用可以正常工作

> *** Error in `./zBunny.exe': free(): invalid pointer: 0x00007f0e528dab78 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f0e5258d7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f0e5259637a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f0e5259a53c]
./zBunny.exe[0x4016e8]
./zBunny.exe[0x401ef2]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f0e52536830]
./zBunny.exe[0x401569]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:10 33423400                           /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00604000-00605000 r--p 00004000 08:10 33423400                           /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
00605000-00606000 rw-p 00005000 08:10 33423400                           /media/keifer/Storage/cppProjects/examples/bunny/zBunny.exe
01f52000-01f84000 rw-p 00000000 00:00 0                                  [heap]
7f0e4c000000-7f0e4c021000 rw-p 00000000 00:00 0 
7f0e4c021000-7f0e50000000 ---p 00000000 00:00 0 
7f0e5220d000-7f0e52315000 r-xp 00000000 fc:01 5509816                    /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52315000-7f0e52514000 ---p 00108000 fc:01 5509816                    /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52514000-7f0e52515000 r--p 00107000 fc:01 5509816                    /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52515000-7f0e52516000 rw-p 00108000 fc:01 5509816                    /lib/x86_64-linux-gnu/libm-2.23.so
7f0e52516000-7f0e526d6000 r-xp 00000000 fc:01 5509806                    /lib/x86_64-linux-gnu/libc-2.23.so
7f0e526d6000-7f0e528d6000 ---p 001c0000 fc:01 5509806                    /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528d6000-7f0e528da000 r--p 001c0000 fc:01 5509806                    /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528da000-7f0e528dc000 rw-p 001c4000 fc:01 5509806                    /lib/x86_64-linux-gnu/libc-2.23.so
7f0e528dc000-7f0e528e0000 rw-p 00000000 00:00 0 
7f0e528e0000-7f0e528f6000 r-xp 00000000 fc:01 5509743                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e528f6000-7f0e52af5000 ---p 00016000 fc:01 5509743                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af5000-7f0e52af6000 rw-p 00015000 fc:01 5509743                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0e52af6000-7f0e52c68000 r-xp 00000000 fc:01 3670249                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52c68000-7f0e52e68000 ---p 00172000 fc:01 3670249                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e68000-7f0e52e72000 r--p 00172000 fc:01 3670249                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e72000-7f0e52e74000 rw-p 0017c000 fc:01 3670249                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0e52e74000-7f0e52e78000 rw-p 00000000 00:00 0 
7f0e52e78000-7f0e52e9e000 r-xp 00000000 fc:01 5509685                    /lib/x86_64-linux-gnu/ld-2.23.so
7f0e53073000-7f0e53079000 rw-p 00000000 00:00 0 
7f0e5309c000-7f0e5309d000 rw-p 00000000 00:00 0 
7f0e5309d000-7f0e5309e000 r--p 00025000 fc:01 5509685                    /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309e000-7f0e5309f000 rw-p 00026000 fc:01 5509685                    /lib/x86_64-linux-gnu/ld-2.23.so
7f0e5309f000-7f0e530a0000 rw-p 00000000 00:00 0 
7ffe1d800000-7ffe1d821000 rw-p 00000000 00:00 0                          [stack]
7ffe1d9b0000-7ffe1d9b2000 r--p 00000000 00:00 0                          [vvar]
7ffe1d9b2000-7ffe1d9b4000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

完整代码:

#include<iostream>
#include<fstream>
#include<vector>
#include<string>

using std::vector;
using std::string;
using std::cout;
using std::endl;


class Node
{
    public:
        int data;
        string name;

        Node* next;
        Node* prev;
};

class Nodes{
    private:
        Node* head = NULL;

    public:
        int insert(); 
        void display();
        int randomGen(int n);
        string getName();
        void extract(std::vector<string> *vect, string fileName);

};

int Nodes::insert()
{
    Node* new_node = (Node*) malloc(sizeof(Node));

    new_node->name = getName();

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

    if (head != NULL)
    {
        head->prev = new_node;
    }

    head = new_node;

    return 0;
}


int main()
{
    Nodes control;
    control.insert();
    control.insert();
    // control.insert();
    // control.insert();
}

2 个答案:

答案 0 :(得分:3)

您正在使用“ malloc”创建一个Node及其内部的std::string,但这是行不通的。您需要改用new。除此之外,所有其他行为都是未定义的行为。 new调用构造函数,这是设置对象以使其正常工作所必需的。

除此之外,您不会使用Node在析构函数中清理delete分配,因此在程序结束时它们会“泄漏”-尽管OS会以任何方式回收内存。

答案 1 :(得分:0)

在C ++中不要使用malloc / free。改用new / delete / delete []。