GUI Problem

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
silentpolygon
Posts: 8
Joined: Sat Aug 26, 2006 6:05 pm
Location: Germany
Contact:

GUI Problem

Post by silentpolygon »

Hi everybody

I´m just playing around with the GUI-System of Irrlicht 1.1. Here is what I have
--------------------------

#include <iostream>
using namespace std;
#include <irrlicht.h>
using namespace irr;

IrrlichtDevice *device = 0;
s32 cnt = 0;

class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();
gui::IGUIEnvironment* env = device->getGUIEnvironment();

switch(event.GUIEvent.EventType)
{
case gui::EGET_BUTTON_CLICKED:

if (id == 101)
{
env->addFileOpenDialog(L"Please choose a file.");
return true;
}
break;
}
}
return false;
}
};

int main()
{
IrrlichtDevice *device = createDevice(video::EDT_NULL,
core::dimension2d<s32>(800,600),
false,false,false,0);

video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* env = device->getGUIEnvironment();

// HardwareMouse is enabled
device->getCursorControl() -> setVisible(true);

MyEventReceiver receiver;
device->setEventReceiver(&receiver);
device->setWindowCaption(L"Naphilia Test Area");

env->addButton(core::rect<s32>(20,210,100,240), 0, 101, L"Open File");

while (device->run())
{
driver->beginScene(true, true, video::SColor(0,0,0,0));

smgr->drawAll();
env->drawAll();

driver->endScene();
}

device->drop();
return 0;
}

---------------------
It Compiles with gcc (Codeblocks) and everything is fine except the thing, that, when i bring the MouseCursor over the Button, Windows shows up an error and closes the app.

Could need some help here.

Thanks in advance
stodge
Posts: 216
Joined: Fri Dec 05, 2003 5:57 pm

Post by stodge »

Use the debugger.
silentpolygon
Posts: 8
Joined: Sat Aug 26, 2006 6:05 pm
Location: Germany
Contact:

Post by silentpolygon »

Thanks
But this is the first time I use the debugger. This is what it says:

Program received signal (SIGSEGV)
Segmentation fault
Previous frame inner to this frame (corrupt stack?)
error


So what could it be?
The World is OpenSource if you have an IrrLicht that leads you...
EmmanuelD
Posts: 25
Joined: Thu Aug 24, 2006 8:34 am
Location: Right in front of Dagoth Ur
Contact:

Post by EmmanuelD »

silentpolygon wrote:Thanks
But this is the first time I use the debugger. This is what it says:

Program received signal (SIGSEGV)
Segmentation fault
Previous frame inner to this frame (corrupt stack?)
error


So what could it be?
You don't verify any pointer you get. createDevice() might return NULL (although this is not the case here), and event.GUIEvent.Caller might be NULL too (thus calling getID() on it will crash).

Compilation is not a proof that your program will run correctly - that's why you have to use a debugger when something goes wrong. But the first thing to do is to write code that is robust - ie code that has no apparent reason to crash - and your's is not.

Murphy's law about this: if a pointer can't be NULL, be sure that at some point ot will be NULL :)

Regards,
-- Emmanuel D.
silentpolygon
Posts: 8
Joined: Sat Aug 26, 2006 6:05 pm
Location: Germany
Contact:

Post by silentpolygon »

EmmanuelD wrote:
silentpolygon wrote:Thanks
But this is the first time I use the debugger. This is what it says:

Program received signal (SIGSEGV)
Segmentation fault
Previous frame inner to this frame (corrupt stack?)
error


So what could it be?
You don't verify any pointer you get. createDevice() might return NULL (although this is not the case here), and event.GUIEvent.Caller might be NULL too (thus calling getID() on it will crash).

Compilation is not a proof that your program will run correctly - that's why you have to use a debugger when something goes wrong. But the first thing to do is to write code that is robust - ie code that has no apparent reason to crash - and your's is not.

Murphy's law about this: if a pointer can't be NULL, be sure that at some point ot will be NULL :)

Regards,
Thank you, EmmanuelD

OK, so The BAckTrace function of Code::Blocks seems to be not very usefull to me. HOW do I verify a pointer??
The World is OpenSource if you have an IrrLicht that leads you...
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Code: Select all

if (pointer==0) doSomethingOnError();
else doSomethingWithGoodPointer();
silentpolygon
Posts: 8
Joined: Sat Aug 26, 2006 6:05 pm
Location: Germany
Contact:

Post by silentpolygon »

hybrid wrote:

Code: Select all

if (pointer==0) doSomethingOnError();
else doSomethingWithGoodPointer();
I think I should remind you that you are speaking with a n00b. The Pointer should be IEventReceiver?

Do I have to put this code into the main()?
The World is OpenSource if you have an IrrLicht that leads you...
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The problem is that you have two device variables. One at global scope, and one inside main. When you call createDevice() you assign to the one in main, but the global one is still left pointing to garbage. You then use the garbage device pointer to call getGUIEnvironment(), which crashes.

Code: Select all

/*IrrlichtDevice **/device = createDevice(video::EDT_NULL, 
EmmanuelD
Posts: 25
Joined: Thu Aug 24, 2006 8:34 am
Location: Right in front of Dagoth Ur
Contact:

Post by EmmanuelD »

vitek wrote:The problem is that you have two device variables. One at global scope, and one inside main. When you call createDevice() you assign to the one in main, but the global one is still left pointing to garbage. You then use the garbage device pointer to call getGUIEnvironment(), which crashes.

Code: Select all

/*IrrlichtDevice **/device = createDevice(video::EDT_NULL, 
Good catch - I didn't even see this. Obviously, this is the root of all evil in the OP's program.
-- Emmanuel D.
Post Reply