Basic code division causing a crash

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
anakin312
Posts: 3
Joined: Wed Jan 28, 2015 4:17 pm

Basic code division causing a crash

Post by anakin312 »

Hi mates,

I am new to Irrlicht (1.8.1) and followed the tutorials. Actually it bothered me to keep working on just the main function, so I tried a little something with my own class for a GUI, with init/run/close methods to do the actual job. Problem is: using my run() in the main makes it crash at runtime (while not if called in the end of init()), and removing the event management makes it work again too, which I definitely can't. Here is the technical part (CMake aside), reduced to the minimum:

Code: Select all

 
#include <irrlicht/irrlicht.h>
 
using namespace irr;
 
class MyEventReceiver : public IEventReceiver {
 
public:
 
  virtual bool OnEvent(const SEvent& event) {
 
    return false;
 
  }
 
};
 
class MyGUI {
 
private:
 
  IrrlichtDevice*       _device;
  video::IVideoDriver*  _driver;
 
public:
 
  MyGUI();
  ~MyGUI();
 
  void init();
  void run();
  void close();
 
};
 
MyGUI::MyGUI() :
  _device(0),
  _driver(0) {
 
  init();
 
}
 
MyGUI::~MyGUI() {
 
  close();
 
}
 
void MyGUI::init() {
 
  _device = createDevice(video::EDT_OPENGL, core::dimension2du(800, 600), 32);
  _device->setWindowCaption(L"A simple GUI to test");
  _device->setResizable(false);
 
  _driver = _device->getVideoDriver();
 
  MyEventReceiver receiver;
  _device->setEventReceiver(&receiver);
 
}
 
void MyGUI::run() {
 
  while (_device->run() && _driver) {
 
    _driver->beginScene(true, true, video::SColor(0, 200, 200, 200));
    _device->getGUIEnvironment()->drawAll();
    _driver->endScene();
 
  }
 
}
 
void MyGUI::close() {
 
  _device->drop();
 
}
 
int main(int, char**) {
 
  MyGUI engine;
  engine.run();
 
  return 0;
 
}
 
In the output we can read "pure virtual method called. terminate called without an active exception", which should be a clue but not to a beginner like me, as I failed at finding those lines in the source.
zerochen
Posts: 273
Joined: Wed Jan 07, 2009 1:17 am
Location: Germany

Re: Basic code division causing a crash

Post by zerochen »

hi,

the problem is in your init function. after you leave it the receiver obj will be destroyed. so irrlicht tries to access a non-existing obj and crashs.

regards
zerochen
anakin312
Posts: 3
Joined: Wed Jan 28, 2015 4:17 pm

Re: Basic code division causing a crash

Post by anakin312 »

Hi zerochen,

Thanks for the prompt reply. You are totally right, changing it to a pointer inside the class makes it work (it needs a new/delete though).

As many of the Irrlicht methods, I expected setEventReceiver, called by the device, to make a copy of it or give the ownership of the object. But it is actually called by address, so I was wrong.

Speaking of events, I'd like to add one more question here: what kind of event is "closing the window using the X"? It made the program crash as well before, so it is definitely an event, but after trying a switch I found out that it was not a "GUI event". What is its category?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Basic code division causing a crash

Post by CuteAlien »

Hm, I think it is a gui-event - try with EGET_ELEMENT_CLOSED.
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
anakin312
Posts: 3
Joined: Wed Jan 28, 2015 4:17 pm

Re: Basic code division causing a crash

Post by anakin312 »

Well, EGET_ELEMENT_CLOSED did not react when closing the window using "X".
But it did with a simple button for which the OnEvent function returns true.
Still a mistery, but we are getting beyond the first topic. Thanks for your help.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Basic code division causing a crash

Post by CuteAlien »

Ah wait - are we talking about a window from Irrlicht-GUI or the application window itself? The latter is not part of the Irrlicht-gui but from the operating system. Irrlicht can't prevent that (same as if you press alt+f4).
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