I was following this tutorial on creating custom CEGUI widgets:
http://www.cegui.org.uk/wiki/index.php/ ... istboxItem
During close if my widget was on the screen it was causing quite a nasty crash.
Ok let me set the scene. We have a GUIManager that is a singleton. During GUIManager::initialisation() among other things we call our version of bindCEGUI which is something like this:
Code: Select all
void bindCEGUI()
{
CEGUI::WindowFactoryManager& wfMgr = CEGUI::WindowFactoryManager::getSingleton();
wfMgr.addFactory(&CEGUI_WINDOW_FACTORY(CheckListboxItem));
wfMgr.addFalagardWindowMapping("WindowsLook/CheckListboxItem", "CEGUI/ItemEntry", "WindowsLook/CheckListboxItem", "Falagard/ItemEntry");
}
For this code to work the WindowFactoryManager has to be created, and therefore this function has to be called after the CEGUI::System has been created.
Our GUIManager is created first to create CEGUI then as part of the initialisation and after CEGUI has been created the custom widget factory singleton is created.
The problem is this. Singletons are deleted in the reverse order in which they are created. When they are destroyed in reverse the custom widget factory goes first, followed by the GUIManager. The GUIManager then tries to destroy all windows in the system through their coresponding factories, however the custom widget factory has already been destroyed resulting in a "pure virtual function call" crash (which was hard to pinpoint).
A workaround we are using at the moment is having a static function in the guimanager called preinitialisation() that calls get singleton on all the custom CEGUI widget factories and is designed to be called before the GUIManager::getSingleton(), however this doesn't seem like a very elegant solution, as it relies on the user to have the correct initialisation sequence to allow custom widgets to work safely.
My suggestion would be to take a more OO approach and provide the window factory manager with an instance of the factory as apposed to a function pointer. Then the order of destruction of singletons would be less of an issue as CEGUI would be maintaining its own copy of the factories and not relying on the user.
I will be interested in hearing other peoples thoughts on the matter:).
ER.