[SOLVED] IrrlichtDevice Segmentation Fault

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
Mitchell M
Posts: 15
Joined: Sat Jul 06, 2013 2:18 pm

[SOLVED] IrrlichtDevice Segmentation Fault

Post by Mitchell M »

Sorry if this is a really dumb question but just when I thought I finally understood pointers I get this problem. Basically what I am having trouble with is that I don't understand how the createDevice works. If I create an IrrlichtDevice *irrlichtDevice and pass that pointer to some function and try to have that function irrlichtDevice = createDevice() then for some reason I can only irrlichtDevice from within that function and if I were to try to do something like while(irrlichtDevice -> run()) in main it gives me a segmentation fault. Is there a way around this. Does the device somehow go out of scope when that function ends?

Basically my problem is this

Code: Select all

 
//main.cpp
IrrlichtDevice *irrlichtDevice;
 
initializer = new Initializer;
initializer -> initializeMasterObjects(irrlichtDevice);
 
//segmentation fault here 
while(device -> run())
{}
 
//Initializer.h
class Initializer
{
    initializerMasterObjects(IrrlichtDevice *irrlichtDevice)
   {
       irrlichtDevice = createDevice( EDT_OPENGL, dimension2d<u32>(800, 600), 16, false, false, false, 0);
   }
 
}
 
Any help would very much be appriciated.
Last edited by Mitchell M on Tue Oct 29, 2013 8:13 pm, edited 1 time in total.
zerochen
Posts: 273
Joined: Wed Jan 07, 2009 1:17 am
Location: Germany

Re: IrrlichtDevice Segmentation Fault

Post by zerochen »

hi,

yes you are right. you are passing a pointer that points to nothing to the method so it cant "pushed back" to the main scope.

here are 2 alternatives:
first let the method return the created device

second you have to pass a pointer to a pointer like:

Code: Select all

 
//usage
...
initializer -> initializeMasterObjects(&irrlichtDevice);
...
 
//implemetation
...
    initializerMasterObjects(IrrlichtDevice **irrlichtDevice)
   {
       if(irrlichtDevice)
       {
             *irrlichtDevice = createDevice( EDT_OPENGL, dimension2d<u32>(800, 600), 16, false, false, false, 0);
       }
   }
...
 
regards
zerochen
Mitchell M
Posts: 15
Joined: Sat Jul 06, 2013 2:18 pm

Re: IrrlichtDevice Segmentation Fault

Post by Mitchell M »

OK I see now thank you so much for your advice and quick response.

Mitchell
Post Reply