[solved]putting createDevice in the constr. is a bad idea?

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
Roland
Posts: 11
Joined: Tue Jun 05, 2012 2:08 pm

[solved]putting createDevice in the constr. is a bad idea?

Post by Roland »

Dear Forum,

I am fairely new to irrlicht and I have a problem which I cant understand. Starting from Tutorial Example 4, I wanted to put the gui example in a proper window-class. So I seperated the while-loop for drawing the window from the definition. This somehow messed everything up. After some try and error, I seem to have identified the problem: it is not possible to call "createDevice" in the constructor of a class and use the returning device instance in an other function. I guess its a local variable that gets out of scope as soon as the constructor is left. Example:

Code: Select all

 
TestGUI::TestGUI()
{
        this->device = createDevice(video::EDT_OPENGL, core::dimension2d<u32>(640, 480), 32, false, false, false, this);
 
        ...
}
 
void TestGUI::runGUI()
{
        while(this->device->run())
        {
                ...
        }
}
 
I call this with

Code: Select all

 
TestGUI test = TestGUI();
test.runGUI();
 
If I copy everything from the constructor to runGUI, it works like a charm. But in this version it does not. Is this a sensible thing to seperate the code this way or am I completely off the hook here?

edit: solved
Last edited by Roland on Tue Jun 12, 2012 12:14 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9718
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: putting createDevice in the constructor is a bad idea?

Post by CuteAlien »

"device" seems to be a membervariable so it's scope is bound to the class. Which means it stays valid as long as the class object itself. Which means your problem is somewhere else. Do you create your TestGUI object globally maybe? Then the constructor will be called _before_ main which might (or might not depending on the rest of your code) cause troubles.
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
Roland
Posts: 11
Joined: Tue Jun 05, 2012 2:08 pm

Re: putting createDevice in the constructor is a bad idea?

Post by Roland »

Ok, I guess I know whats hapening. My last C++ was 5 years ago and since then, switching to Java did not made my understadning of heap and stack memory any better. What i did was, to create an object holding the TestGUI class, implicitly calling the constructor of TestGUI. Then, I called an initializer function for TestGUI, which created just an other object, but putting it on the stack, which got lost after the initializer function was finished. When I called runGui afterwards, the reference was out of scope and therefore, mysterious errors occured. I cant have the TestGUI being initialized in the constructor of the wrapper class, so I have to use heap memory. I thought I could avoid it and didnt understand why it wasnt working. Guess I was wrong. Nothing wrng with Irrlicht though.

As much as I love the control, c++ gives me over the momory, its such a mess because there are far to few warnings if something goes wrong. Have to get used to it again. Thats the beginners forum, right? I dont need to be embarrassed so much..
CuteAlien
Admin
Posts: 9718
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: putting createDevice in the constructor is a bad idea?

Post by CuteAlien »

Don't worry, people fighting with memory problems are countless in this forum ;-)
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