I am using virtual inheritance with a selection of classes in c++. It is currently crashing on destruction. It seems to compile fine in the online compilers, however when I run in Visual Studio, it crashes.
I have a pure virtual base class, which is being inherited virtually by its implementation. I then have a third class that is inheriting from the implementation regularly. I am using an internal system for creating and releasing memory. Under the hood it is using a placement new with a aligned malloc. It is then using free to free the memory. I have created this minimum example. It is not exactly what I am doing but I seem to get a similar problem.
#include <iostream>
#include <string>
int main()
{
class Animal {
public:
Animal() { }
virtual ~Animal() { }
virtual void eat() { }
};
class Mammal : public virtual Animal {
public:
virtual void breathe() { }
};
class WingedAnimal : public virtual Animal {
public:
virtual void flap() { }
};
// A bat is a winged mammal
class Bat : public Mammal, public WingedAnimal {
};
Animal* bat = new(malloc(sizeof(Bat))) Bat;
bat->~Animal();
free(bat);
printf("Done!");
}
Like I said, this example will print "Done" in the online compiler. However in Visual Studio 2015 it seems to crash on free of the bat object. I am fairly new to virtual inheritance and placement new. Does anyone see the problem?