Complex C ++ inheritance and smart kill of smart pointer cause heap corruption in VS 2017

I ran into a problem with the VS debugger with the above code:

class Animal {
public:


};


class Stupid {
public:

};


class Dog : public Stupid, public Animal {
public:


};

int main() {
    std::unique_ptr<Animal> animal = std::unique_ptr<Dog>(new Dog());
    animal.reset();
    return 0;
}

This code throws an error after executing "animal.reset ()" involving "ntdl.dll" and "wntdll.pdb".

Here are the expressions that generated the assertion errors using the MSVC runtime library if I click ignore several (3) times:

1- _CrtIsValidHeapPointer(block)
2- is_block_type_valid(header->_block_use)
3- HEAP CORRUPTION DETECTED: before Free block (#-50331640) at 0x03737E21. CRT detected that the application wrote to memory before start of heap buffer.

But if I change the inheritance of Dog, like this:

class Dog : public Animal, public Stupid {
public:


};

The code is working fine.

I only have this error in visual studio 2017, I tried with Ideone , Android Studio, and it works fine, regardless of the order of inheritance.

+4
source share
1

, , delete, , new.

- , - , . , , . !

, , , , , , . , :

Dog* d = new Dog;
Animal* a = d;

a d.

, , , delete (Animal ). , delete.

, gcc , . . , .

+6

Source: https://habr.com/ru/post/1693649/


All Articles