-1

I created a linked list called ScanList containing nodes of object Scan. But when I remove the first Scan from the linked list and close the program, I get an exception of type "Access violation reading location" at the destructor of ScanList.

The function where I remove Scan from the ScanList is below.

bool ScanList::removeScan(int serialNumber)
{// post: IF serialNumber is present in the list 
 // THEN scan has been removed from it and true has been returned 
 // ELSE false has been returned 
    Scan* currentScan = scans;
    Scan* previousScan = scans;

    if (currentScan != NULL && currentScan->getSerialNumber() == serialNumber)
    { // if serialNumber is the first element of the linked list
        *scans = currentScan->getNext();
        delete currentScan;
        return true;
    }

    while (currentScan != NULL && currentScan->getSerialNumber() != serialNumber)
    {
        *previousScan = currentScan;
        currentScan = currentScan->getNext();
    }

    if (currentScan == NULL)
    { // serialNumber is not present in the list
        return false;
    }

    // serialNumber is present in the list and is removed
    previousScan->setNext(currentScan->getNext());
    delete currentScan;
    return true;
}

I use a Copy assignment operator, but even then it removes scans. It does return the correct values, but when I delete the currentScan it also removes the whole Scan* scans of the ScanList. This is my copy assignment operator.

Scan* Scan::operator=(const Scan* scan)
{
    // check if the Scan on the left is the same as Scan on the right
    if (this == scan)
    { // if they are the same, go out of the function and return
        return this;
    }

    this->serialNumber = scan->serialNumber;
    this->timesRecycled = scan->timesRecycled;
    this->next = scan->next;

    return this;
}

Can anyone tell me what I am doing wrong?

Esmee
  • 51
  • 8
  • 1
    Concerning `*previousScan = currentScan;`: This does compile without complaints? Either it's a typo in your question or the possible issue you're asking for. Reading your question again, I tend to the latter. (Your `Scan::operator=(const Scan* scan)` is begging for this.) An idiomatic assignment operator should be `Scan& operator=(const Scan&)`. Then `*previousScan = currentScan;` would be recognized as compiler error. I'm quite sure what you actually intended was `previousScan = currentScan;`. – Scheff's Cat May 11 '21 at 15:21
  • What is `scans`? Is it a pointer to a `Scan`? If so, then what do you intend to do when you assign a value to `*scans`? – Tim Randall May 11 '21 at 15:25
  • @TimRandall scans is indeed a pointer to a Scan, assigning a value to *scans is to set the first element of the linked list since the original first element was removed. – Esmee May 11 '21 at 15:51
  • @Scheff When I change the assignment operator to ```Scan& Scan::operator=(const Scan& scan)``` and also change to ```previousScan = currentScan``` then it still crashes.. – Esmee May 11 '21 at 15:57
  • Then, you might have yet another problem. I debugged by eyes, and it might be that I overlooked something. You have the better opportunity to use a [Debugger](https://stackoverflow.com/q/25385173/7478597) on your code. – Scheff's Cat May 11 '21 at 16:04
  • 1
    Please update your question and add the code that creates a list, then calls this function, triggering the error. We should get enough code to reproduce the error. – trincot May 11 '21 at 17:50

1 Answers1

0

The problem was solved, I was deleting the nextScan for each Scan in the Scan destructor which resulted in the whole list getting deleted. Thanks for your suggestions!

Esmee
  • 51
  • 8