[SOLVED] segmentation fault when trying to cast IGUIElement to

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
fargoth
Posts: 22
Joined: Fri May 01, 2015 7:43 pm

[SOLVED] segmentation fault when trying to cast IGUIElement to

Post by fargoth »

I'm trying to retrieve the value selected on a menu.

The following line gives seg fault on

Code: Select all

dynamic_cast<std::remove_pointer_t<T> *>(guiElement);
From here:

Code: Select all

                std::remove_pointer_t<T> * getGuiElement(int in_id)
		{
			//check that T is a valid type to try to upcast during compile time.
			static_assert(std::is_base_of<irr::gui::IGUIElement, std::remove_pointer_t<T>>::value, 
				"T must inherit irr::gui::IGUIElement");
			if (!_guiEnv)
			{
				//LOG(ERROR) << "gui environment is null";
				throw std::runtime_error("menu was not set correctly!");
			}
			if (!_guiEnv->getRootGUIElement())
			{
				throw std::runtime_error("no gui root element");
			}
			auto guiElement = _guiEnv->getRootGUIElement()->getElementFromId(in_id);
			if (!guiElement)
			{
				throw std::runtime_error("Could not find gui element");
			}
			auto requestedElement = dynamic_cast<std::remove_pointer_t<T> *>(guiElement);
			if (!requestedElement)
			{
				throw std::runtime_error("Failed dynamic casting of GUI element");
			}
			return requestedElement;
		}
It gets called like this:

Code: Select all

getGuiElement<irr::gui::IGUISpinBox>(MenuElement::MazeNumOfAI)->getValue();
It was created like so:

Code: Select all

		auto spinBox = _guiEnv->addSpinBox(L"Number of AI players", irr::core::recti(elementPosition, elementSize),true,nullptr,MenuElement::MazeNumOfAI);
		spinBox->setDecimalPlaces(0);
		spinBox->setRange(0.0f, 10.0f);
		spinBox->setValue(3.0f);
		_gameSettingsElements.push_back(spinBox);
Any idea what may be the problem?
Last edited by fargoth on Wed Jan 10, 2024 9:43 pm, edited 1 time in total.
fargoth
Posts: 22
Joined: Fri May 01, 2015 7:43 pm

Re: segmentation fault when trying to cast IGUIElement to

Post by fargoth »

Ah, I've found it :) Needed to disable RTTI and use static_cast instead of dynamic_cast...
Noiecity
Posts: 163
Joined: Wed Aug 23, 2023 7:22 pm
Contact:

Re: [SOLVED] segmentation fault when trying to cast IGUIElement to

Post by Noiecity »

great
**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free. CC0 man.

Image
**
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [SOLVED] segmentation fault when trying to cast IGUIElement to

Post by CuteAlien »

You don't have to disable RTTI in your own project. But Irrlicht indeed compiles without RTTI, so dynamic_cast on Irrlicht pointers will always fail.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply