1

So I got a class A and class B where class A is the base class of class B. However when I do something like this:

int main()
{
   B der(222);
   A* test;
   test = &der;
   delete test;
}

I got an error:

debug assertion failed!

program:.......
line 52 

Expression:_BLOCK_TYPE_IS_VALID(pHead0>nBlockUse)

any idea?

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
smith Neil
  • 155
  • 1
  • 7

2 Answers2

3

You can't call delete on a variable that you didn't manually allocate memory for.

David Kiger
  • 1,958
  • 1
  • 16
  • 25
2

In your code der is not a dynamically allocated object. ie, it is not allocated using new. So you should not use delete on that object. der object would be automatically destroyed once it goes out of scope. If you want to use delete, then you should create the object using new

A * test = new B(222);
delete test;
Sanish
  • 1,699
  • 1
  • 12
  • 21