0

Im trying to create a GUI using the library Irrlicht in C++, however whilst wring my Gui Handler, I get SIGSEGV errors when trying to access any data outside the constructor.

My header looks like this:

#pragma once
#include <irrlicht.h>
#include "Structs.h"
#include <vector>
#include <string>
class GuiHandler
{
public:
    GuiHandler(Structs::SAppContext *contIN);
    ~GuiHandler();

    bool GuiEvent(const irr::SEvent& eventType);
    struct RGBPicker
    {
        IGUIButton* sample;
        IGUIStaticText* RGB[3];
        IGUIEditBox* RGBValues[3];
        int Colour[3];
    };
    RGBPicker bottomColour;
    RGBPicker topColour;
    int i;
}

The values are assigned to the 2 RGBPicker variables in the constructor in the cpp file:

GuiHandler::GuiHandler(Structs::SAppContext *contIN)
{
    context = contIN;

    int xpos = 185;
    int ypos= 110;
    //bottomColour.pickerTexture->fill(s)
    bottomColour.sample = context->env->addButton(rect<s32>(128+xpos, 16+ypos, 198+xpos, 86+ypos), 0, 111, L"");
    bottomColour.sample->setEnabled(false); //static box
    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");
    }
    bottomColour.RGBValues[0]->setToolTipText(L"B1");
    bottomColour.RGBValues[1]->setToolTipText(L"G1");
    bottomColour.RGBValues[2]->setToolTipText(L"R1");

    bottomColour.RGB[0] = context->env->addStaticText(L"B:", rect<s32>(48+xpos, 64+ypos, 66+xpos, 84+ypos), false, false, 0, -1,114);
    bottomColour.RGB[1] = context->env->addStaticText(L"G:", rect<s32>(48+xpos, 40+ypos, 66+xpos, 60+ypos), false, false, 0, -1,114);
    bottomColour.RGB[2] = context->env->addStaticText(L"R:", rect<s32>(48+xpos, 16+ypos, 66+xpos, 36+ypos), false, false, 0, -1,114);


    std::cout<<i<<std::endl;
    i=2;
    std::cout<<i<<std::endl;

}

Whilst everything works within the header- accesing all the topColour, bottomColour and context statements, when in any other method within the class, they result in a SIGSEGV. Even printing out i with cout. I know i must be doing very simple incorrectly however I can't work it out.

Thanks.

Dom M
  • 1
  • 2
  • 1
    You need to init the pointers `IGUIButton* sample; IGUIStaticText* RGB[3]; IGUIEditBox* RGBValues[3];` – JLev Jul 16 '17 at 07:13
  • What do you mean by to Init them? as in include `IGUIButton* sample;` within the cpp file? I added this, and it makes no difference. I already did this for the integer and it still happens. – Dom M Jul 16 '17 at 07:33
  • `IGUIButton* sample = new IGUIButton(...);`. You need to allocate the memory for this objects – JLev Jul 16 '17 at 08:02
  • addGUIButton is defined as `virtual IGUIEditBox * addEditBox (const wchar_t *text, const core::rect< s32 > &rectangle, bool border=true, IGUIElement *parent=0, s32 id=-1)=0`. would this not allocate the memory in this case? since its a pointer, not a copy of the element? I still cant work out why the standard integer is inaccessible through other methods though – Dom M Jul 16 '17 at 08:25
  • It would allocate. If you debug, can you see what addEditBox returns? check that it's not a null pointer. – JLev Jul 16 '17 at 09:04
  • It's not a null pointer. I use `bottomColour.RGBValues[i]->setMax(3);` within the ctor and it works perfectly. Its just when it goes out of scope from the ctor. Other functions within the class try to access it and it pulls a segfault – Dom M Jul 16 '17 at 12:32
  • We need to see the implementation of addEditBox to make progress on this – mksteve Jul 16 '17 at 14:50

0 Answers0