MChess404
MChess404

Reputation: 13

Linked list with memory leak issues

This program is acting as a template for what will be a linked list of bunny objects with unique names and colors.

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;
}

The unique attributes are assigned from returned function calls on creation of the node(bunny) when its inserted to the linked list.

new_node->name = getName();

The function call works fine until I call it for the creation of multiple nodes resulting in this error:

> *** 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

FULLCODE:

#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();
}

Upvotes: 1

Views: 47

Answers (2)

FrantzX
FrantzX

Reputation: 62

Don't use malloc / free in C++. Use new / delete / delete [] instead.

Upvotes: 0

xaxxon
xaxxon

Reputation: 19761

You are creating a Node and the std::string inside it with "malloc" and that doesn't work. You need to use new instead. Anything beyond that is undefined behavior. new calls the constructors, which is required for the objects to be set up to work correctly.

Beyond that, you aren't cleaning up your Node allocations in a destructor using delete, so at the end of the program they are "leaked" - though the OS will reclaim the memory anyhow.

Upvotes: 3

Related Questions