So I have a self-implemented linkedlist class. For the copy constructor which makes a "deep copy" or basically a copy of the linkedlist. But for the assignment operator, I have basically done this
// assignment operator
linked_list& linked_list::operator=(const linked_list& L){
// check for self assignment
if(this != &L){
this->head=L.head;
this->sz=L.sz;
}
return *this;
}
This makes a "shallow copy", so when I make a new linkedlist b and point it to a, changes in b will be reflected in a.
But now the issue is, when I call a destructor which dynamically allocates the nodes, since both of them point reference the same object, and one of the destructors gets called before the other, I get the following error message.
edit.out(3537,0x106bcbdc0) malloc: *** error for object 0x7fa430c018f0: pointer being freed was not allocated
edit.out(3537,0x106bcbdc0) malloc: *** set a breakpoint in malloc_error_break to debug Abort trap: 6
I used lldb to probe any solutions, but it is beyond my control to estimate which objects destructor will be called first, and how I can prevent it from being called again.