-3

I know exactly where this is error is being caused, and I think I might even have a clue as to why—but I can't figure out what's doing it:

Room

// room.hpp

#include "linkedlist.hpp"
#include "item.hpp"

class item;
class Room {
    public:
        Room();

    private:
        LinkedList<Item> itemsInThisRoom_;
};

-

// room.cpp

#include "room.hpp"
#include <iostream>

Room::Room() {
    itemsInThisRoom_ = LinkedList<Item>();
};

LinkedList

// linkedlist.hpp

//====================
// Class (Declaration)

template<class T> struct LinkedList {
    public:
        LinkedList();
        ~LinkedList();

    private:
        Node<T>* head_;
};

//====================
// Class (Instantiation)

template<class T> LinkedList<T>::LinkedList() {
    head_ = new Node<T>;
    size_= 0;
};

template<class T> LinkedList<T>::~LinkedList() { delete head_; };

main.cpp

#include <iostream>
#include "room.hpp"
#include "linkedlist.hpp"

int main() {
    Room room;
    return 0;
};

The error is being called when Room's destructor is called—and only if I initialize a room object. This works fine:

#include <iostream>
#include "room.hpp"
#include "linkedlist.hpp"

int main() {
    Room* room;
    return 0;
};

Also, I can construct/destruct instances of LinkedList by itself without problem. I think that somehow 'head_' is being deleted twice, but I can't figure out how.

(Someone's advised me to start using std::unique_ptr instead of new/delete, which I will start doing, but I just want to figure out why this is causing an error!!)

AmagicalFishy
  • 1,249
  • 1
  • 12
  • 36
  • Might be something in `Node`, which you haven't posted. – user657267 Aug 14 '15 at 08:19
  • how is the 2nd working fine? it isnt even constructing instance of Room – BЈовић Aug 14 '15 at 08:20
  • ...or `Item`, and where is `LinkedList::size`? Post something that actually compiles. – user657267 Aug 14 '15 at 08:21
  • @user657267 I didn't post Node or Item because both of those classes can be swapped for anything else—I whittled the code down to the bare minimum of what was necessary to cause the error (size(), for example, has nothing to do with it). I've been looking for the cause for hours. :l – AmagicalFishy Aug 14 '15 at 08:24
  • 1
    The cause is here: `itemsInThisRoom_ = LinkedList();` You construct a new `LinkedList`, and then assign it to a variable. So copying takes place. After copying, both the temporary `LinkedList` and variable `itemsInThisRoom_` contain the same pointer `head_`. The temporary gets destructed soon, callind `delete head_;`. Then the variable gets destructed too, calling `delete head_;` again. To fix the problem you need to implement a copy constructor in class `LinkedList`, performing a deep copy of the linked list. – Serge Rogatch Aug 14 '15 at 08:25
  • @SergeRogatch No wonder everyone seems to use pointers all willy-nilly! Thanks so much~ – AmagicalFishy Aug 14 '15 at 08:28
  • @AmagicalFishy "the minimal amount of code" on SO is something that we can compile, no more, no less. – user657267 Aug 14 '15 at 08:31

1 Answers1

1

You manually manage memory, which means you also need to implement the assignment operator, and the copy constructor.

Now, when you copy the object: itemsInThisRoom_ = LinkedList<Item>();, you free the memory when the stack object (LinkedList<Item>();) is deleted, but itemsInThisRoom still points to it.

So, when that is destroyed, it tries to delete the memory that was already deleted in the stack object.

Mark Jansen
  • 1,491
  • 12
  • 24