Possible Widget isType bug.

MindCalamity

02-04-2012 21:36:33

I seem to be getting this error when trying to get a button from the GUI:

MyGUI::Button* button = MyGUI::Gui::getInstance().findWidget<MyGUI::Button>("FBTN");
button->setCaption("TEST");


I'm not sure if relevant - but I'm doing this per frame as a test.

Complete error:
22:12:46 | Core | Critical | Error cast type 'Button' to type 'Button' . | d:\libraries\mygui\sdk\include\mygui\MyGUI_IObject.h | 33

Update: Seems that if I do that in the MyGUI demo framework (derive Picking DemoKeeper from Ogre Frame Listener and update the button from there) it works.

There's nothing different in the way I initialize stuff, and the way the demo framework does the same... I can't seem to figure out what the problem is.

Altren

03-04-2012 10:36:06

Well, there is no clear answer what is your problem, but here are few things,
Does this exception raised at first call or at some random point?
Does this exception raised when you call findWidget not per frame, but in some other place?
Check, that you are not using MyGUI from different threads - MyGUI is not designed to work from different threads.

If this happens at first call and easy to reproduce you can replace findWidget with it's internal logic and debug your application. Something like:
MyGUI::Widget* widget = MyGUI::Gui::getInstance().findWidgetT("FBTN");
MyGUI::Button* button = nullptr;
if (widget->isType<Button>())
button = static_cast<Button*>(widget);
else
MYGUI_ASSERT(!_throw, "Error cast type '" << this->getTypeName() << "' to type '" << Type::getClassTypeName() << "' .");

MindCalamity

03-04-2012 12:28:21

Hi, thanks for your reply.

MyGUI::ButtonPtr button1 = MyGUI::Gui::getInstance().createWidget<MyGUI::Button>("Button", 10, 10, 300, 26, MyGUI::Align::Default, "Main", "FBTN");
button1->setCaption("exit");
MyGUI::Widget* widget = MyGUI::Gui::getInstance().findWidgetT("FBTN");
MyGUI::Button* button = nullptr;
if (widget->isType<MyGUI::Button>())
{
std::cout << "It's a button\n";
button = static_cast<MyGUI::Button*>(widget);
}
else
{
std::cout << "It's not a " << button1->getTypeName() << ", it's a " << widget->getTypeName() << "\n";
}


What the heck is wrong with isType ? It always returns 0, even though the widget is always a button, to make sure I ran the above code and got the following output:

It's not a Button, it's a Button

It kind of made me laugh, as I figured out where the problem was, which is great, because now I can just cast the widget into a button:


MyGUI::Widget* widget = MyGUI::Gui::getInstance().findWidgetT("FBTN");
MyGUI::Button* button = nullptr;
button = static_cast<MyGUI::Button*>(widget);
button->setCaption("FPS: " + Ogre::StringConverter::toString(Ogre::Root::getSingleton().getAutoCreatedWindow()->getAverageFPS()));


And it will work, but I still can't check for the widget type, and this is probably a bug in MyGUI, not to mention it will only work if I know the widget type prior to making a query.

Strange enough - it does work with the DemoKeeper classes...

Any ideas how to fix this ?

Altren

03-04-2012 22:25:58

MyGUI use rtti in vaarious places inside. And solving this findWidget problem doesn't mean, that everything works as it should.
What compiler do you use. Do you use some specific compiler options? This might be broken rtti or bug in optimisation.

Also check next code:
if (widget->isType(typeid(MyGUI::Button)))
{
std::cout << "widget is button" << std::endl;
}

if (button1->isType(typeid(MyGUI::Button)))
{
std::cout << "button1 is button" << std::endl;
}
Does both checks failed?