Hi.
Been using SFML for a building a tetris clone for learning purposes. In order to get a start menu, i wanted to add several screens and found this example:
https://github.com/SFML/SFML/wiki/Tutorial:-Manage-different-Screens.
This worked perfectly, but i wanted to make the background and headline the same for all screens, so i have the code for this in the constructor of the abstract base class. Still works perfectly.
The trouble started when adding a destructor for the base class, i have tried several ways:
//in .h:
virtual ~cScreen() = 0;
//in .cpp:
cScreen::~cScreen() { }
or
//in .h:
virtual ~cScreen();
//in .cpp:
cScreen::~cScreen() {};
Or just:
//in .h:
~cScreen();
//in .cpp:
cScreen::~cScreen() {}
I suddenly got strange errors in the destructor for Screen_1 which looks like this:
//.h:
~Screen_1();
//in .cpp:
Screen_1::~Screen_1()
{
for (auto i : texts) delete i;
for (auto i : shapes) delete i;
}
The error comes from trying to delete the contents of texts, in debugging i see that all the members of Screen_1 is given strange values, like the size of texts is 0, and int members which are set to say 5, suddenly has max_int value. Some of the time visual studio says that the heap has been corrupted.
I've tried different ways of creating the objects in main as well
cScreen* s = new Screen_1();
or
Screen_1* s = new Screen_1();
etc.
Without having a destructor in the base class everything works fine, and of course i don't really need it, but i want to understand what is really happening here, and why.
It almost seems as if when the destructor for the derived class is called it is already corrupted or deleted, but when the abstract destructor is pure virtual it is never supposed to be called, is it?
I've tried both with and without actual stuff happening in the abstract destructor body.
Any help would be greatly appreciated.