Search code examples
c++gdbdelete-operator

Why is the delete temp crashing even though the function is defined with new


I am making a modified double ended Linked List, but the head and the tail point to each other. In the insertBeforeCurrent and the insertAfterCurrent I defined the objects with new and put them into the Linked List. But when I go to use delete, the program just crashes. I have done some tests, and insertBeforeCurrent and insertAfterCurrent works, and I am able to through the Linked List and print every single element with the getPrevious and getNext. I also printed it using only insertBeforeCurrent, insertAfterCurrent and I was also able to do the same with the mixture of the two. I was able to print it with 1,2,3,and 6 elements in the Linked List. The problem I have is the debugger, everything works until I hit the delete temp; at which point it will just say. Can't find a source file at "/build/glibc-t7JzpG/glibc-2.30/signal/../sysdeps/unix/sysv/linux/raise.c" Locate the file or edit the source lookup path to include its location. I know you can only use the delete function for delete the data created by the new's dynamic memory allocation, but that doesn't seem to be the case since every element of the Linked List is created by new.

So the issue with application crashing is not the Node * x = new Node(); followed by x = y;. These don't show any warnings,the application runs, and 5 or 6 people have pointed them out. Thank you by the way. My issue is specifically is the delete temp;and why is it not being deleted. I have left the code for some context.

EDIT: I have removed the insertBeforeCurrent and insertAfterCurrent code since it is not needed.


 bool CircularDoublyLinkedList::remove(int original_data)
        {
            Node search_data = search(original_data);
            Node* temp = &search_data;
            Node* current_next;
            Node* current_previous;
                if (temp != NULL)
                {
                        if (temp == head)
                        {
                            current_previous = temp->getPrevious();
                            current_next = temp->getNext();
                            current_previous->setNext(current_next);
                            current_next->setPrevious(current_previous);
                            head = current_next;
                            temp->setNext(NULL);
                            temp->setPrevious(NULL);
                            delete temp;
                            current = current_next;
                            cout << "Delete successful." << endl;
                        }
                        else if (temp == tail)
                        {
                            current_previous = temp->getPrevious();
                            current_next = temp->getNext();
                            current_next->setPrevious(current_previous);
                            current_previous->setNext(current_next);
                            tail = current_next;
                            temp->setNext(NULL);
                            temp->setPrevious(NULL);
                            delete temp;
                            current = current_next;
                            cout << "Delete successful." << endl;
                        }
                        else
                        {

                            current_previous = temp->getPrevious();
                            current_next = temp->getNext();
                            current_previous->setNext(current_next);
                            current_next->setPrevious(current_previous);
                            temp->setNext(NULL);
                            temp->setPrevious(NULL);
                            delete temp;
                        }
                        return true;
                    }
                    return false;
                }

Solution

  • I know you can only use the delete function for delete the data created by the new's dynamic memory allocation,

    So far so good.

    but that doesn't seem to be the case since every element of the Linked List is created by new.

    This is not relevant, since the temp in CircularDoublyLinkedList::remove() never points to an element of the linked list. You assign to temp the address of a local variable (search_data) and never change that. Local variables are not created by new, so (as you noted earlier), you cannot delete &search_data.

    (You might have noticed this yourself had you used a debugger to step through your code while trying to delete the first element of the list. In your current code, temp == head will never be true, even though that is the branch for deleting the first element. Similarly, temp == tail will never be true, and temp != NULL will never be false.)

    At a guess, your search() function should probably return a pointer to a node in the list instead of returning the node, at which point you'll no longer need the (poorly-named) temp variable.