Accessing getText from IGUIEditBox causing SIGSEGV

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
Dominicm99
Posts: 3
Joined: Sat May 27, 2017 4:59 am

Accessing getText from IGUIEditBox causing SIGSEGV

Post by Dominicm99 »

Hi All,
I'm trying to create a GUI for my program. Im using Edit Boxes to input RGB values to change the colour of a button as a rudimentary colour picker, however when trying to access any methods from the IGUIEditBox, I get a segfault. Im not sure why this is the case as it should be accessible (it is in the same class).
The header of the class contains a struct in which all the elements are kept:

Code: Select all

#pragma once
#include <irrlicht.h>
#include "Structs.h"
#include <vector>
#include <string>
class GuiHandler
{
public:
    GuiHandler(Structs::SAppContext *contIN);
    bool GuiEvent(const irr::SEvent& eventType);
    ~GuiHandler();
     struct RGBPicker
    {
        IGUIButton* sample;
        IGUIStaticText* RGB[3];
        IGUIEditBox* RGBValues[3];
        int Colour[3];
    };
    RGBPicker bottomColour;
    RGBPicker topColour;
protected:
    void setSkinTransparency(s32 alpha, irr::gui::IGUISkin * skin);
    Structs::SAppContext *context;
    std::vector<IGUIButton*> buttons;
    std::vector<IGUIListBox*> listboxes;
    std::vector<IGUIComboBox*> comboBoxes;
    std::vector<IGUIStaticText*> labels;
    std::vector<IGUIEditBox*> RGBControls;
    int selectedChannel = 0;
 
 
private:
    bool isNumeric(std::string str);
};
 
And the issue is when trying to access things such as bottomColour.RGBValues[0]->getText(); It results in a return code 255 and a crash

Code: Select all

bool GuiHandler::GuiEvent(const irr::SEvent& event)
{
    s32 id = event.GUIEvent.Caller->getID();
 
        if (event.GUIEvent.EventType == EGET_EDITBOX_CHANGED)
        {
                switch (id)
                {
                case 113:
                    irr::core::stringc text = event.GUIEvent.Caller->getText();
 
                    if (text == L"")
                       {
                           text = L"0";
                           event.GUIEvent.Caller->setText(L"0");
                       }
 
                    std::string ctxt = text.c_str(); //cast irrlicht string into a c++ string for easier analysis
                    if (isNumeric(ctxt))
                    {
                        int val = atoi(ctxt.c_str());
                        if (val > 255) //limit to RGB values
                        {
                            val = 255;
                            event.GUIEvent.Caller->setText(L"255");
                        }
                        //update colourpicker
                        for (int i = 0;i<3;i++)
                        {
                            irr::core::stringc bottomText = bottomColour.RGBValues[i]->getText();
                            std::string bottomTextC = bottomText.c_str();
                            bottomColour.Colour[i] = atoi(bottomTextC.data());
                            std::cout<< bottomColour.Colour[i]<<std::endl;
                        }
                    }
                    else
                    {
                        event.GUIEvent.Caller->setText(L"0");
                    }
                    break;
                }
 
        }
}
 
Note: it works fine in the Constructor, however it does not work in the event handler

Thanks for the Help,
Dom
Dominicm99
Posts: 3
Joined: Sat May 27, 2017 4:59 am

Re: Accessing getText from IGUIEditBox causing SIGSEGV

Post by Dominicm99 »

I'm adding the pointers to the elements into the struct in the constructor, however I'm not sure if that makes a difference as of any other way.

Code: Select all

 int xpos = 185;
    int ypos= 110;
    bottomColour.sample = context->env->addButton(rect<s32>(128+xpos, 16+ypos, 198+xpos, 86+ypos), 0, 111, L"");
    bottomColour.RGBValues[0] = context->env->addEditBox(L"", rect<s32>(64+xpos, 64+ypos, 120+xpos, 84+ypos), true, 0, 113);
    bottomColour.RGBValues[1] = context->env->addEditBox(L"", rect<s32>(64+xpos, 40+ypos, 120+xpos, 60+ypos), true, 0, 113);
    bottomColour.RGBValues[2] = context->env->addEditBox(L"", rect<s32>(64+xpos, 16+ypos, 120+xpos, 36+ypos), true, 0, 113);
    for(int i = 0; i <3; i++)
    {
        bottomColour.RGBValues[i]->setMax(3);
        bottomColour.RGBValues[i]->setText(L"0");
    }
etc.

I've noticed it's the same error as when event.GUIEvent.Element->getText() is called, rather than event.GUIEvent.Caller. Im really at a loss as to why this isn't working

Edit:
It seems that all data in my header variables are out of scope outside of the constructor, but I'm not sure why. i thought if you declare variables in the header, private public or protected, they are able to be accessed at all times throughout the class. Any clarification on this would be great, Thanks
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Accessing getText from IGUIEditBox causing SIGSEGV

Post by CuteAlien »

Hard to help with incomplete code snippets. We can't compile it. We can't see what you are really doing. Put your code on some service like bitbucket or github, then it is easy to share.

Code being in header or .cpp has nothing to do with scope. The pre-processor of the compiler simply copies the content of header files into the cpp files at the place where you have your include, there is nothing special about them (if you want to see what a pre-compiler does use g++ compiler with -E option).

I do not see anything obvious you are doing wrong. My best guess without having your code is that your instance of GuiHandler itself is no longer valid. For example you create a local variable for GuiHandler (and not local in main but in some sub-function) and pass that to the evenreceiver.

Also you should always initialize all pointers to 0 (or nullptr when you are using c++11). It will help you a lot in debugging and there is nearly never a reason not to do that.
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