0

For a class assignment I need to implement a destructor for the objects of linked-list I've created. I made a function called MakeEmpty that I called inside the destructor. It compiled correctly the first time, but now I am getting instant crashes with an error saying:

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

Can someone help me figure out what is wrong? I ran the debugger and pointed out where it says the error is in the code.

WORD::~WORD()
{
cout << "Destructor Called"<<endl;

(*this).MakeEmpty();
}

And this is the MakeEmpty() function

void WORD::MakeEmpty()
{
alpha_numeric *p = (*this).front;

if((*this).IsEmpty())
{
    cout <<"Already empty"<< endl;
    return;
}

while(front != 0)
{
    front = front -> next;
    delete p;//<<<<---DEBUGGER SAYS ERROR HERE
    p = front;
}
return;
}
Servy
  • 202,030
  • 26
  • 332
  • 449
Mike
  • 477
  • 2
  • 7
  • 24
  • 1
    Buddy, You are deleting "p" and then Assigning value to it... How this could be possible!! – Swanand Jun 08 '12 at 05:08
  • @SwanandPurankar I wanted to delete the node that p was pointing to, then advance p to the next node, delete that one, and so on until they are all gone – Mike Jun 08 '12 at 05:09
  • Yes, We got it... But You are missing a small thing there... Check your code carefully...Don't expect answer... It's your homework! – Swanand Jun 08 '12 at 05:11
  • 2
    The loop looks fine to me. You should post a minimal example of a case where you *use* `WORD` and it crashes. Have you written proper copy constructors and assignment operators for both your classes? – molbdnilo Jun 08 '12 at 05:55
  • @molbdnilo I havent done any of those. Could that be the problem? – Mike Jun 08 '12 at 06:18
  • 1
    @MikeGordon: That's almost certainly the problem. If you don't have a copy constructor, then copying a `WORD` will give you two objects pointing to the same list. Both destructors will try to delete that list, resulting in undefined behaviour. If you have a class that manages a resource, then you need to think about the [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – Mike Seymour Jun 08 '12 at 06:34

2 Answers2

3

Since it's a homework I'm not going to give the solution but rather a hint.

while(front != 0)
{
    front = front -> next;
    delete p;//<<<<---DEBUGGER SAYS ERROR HERE
    p = front;
}

In this while, where in the list you start/end the deletion? And why?

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
  • well, the deletion would start at the first node in the list, because p was initialized to this node. And the deletion would end at the last node because p is left behind at the last node once front hits null. But I believe this while loop does that correctly, but apparently it dosent – Mike Jun 08 '12 at 05:13
0

You have a coding error... you need to: Bench check your code!

Write down the expected values of all variables at each statement and through each iteration of the loop. Don't skip any steps. This will make your coding error obvious.

After 30 years I do this in my head.. but I always do it to make sure what I've written is correct. This one habit will serve you well in years to come.

Richard K.
  • 171
  • 1
  • 3