Page 1 of 2

Segmentation fault when loading a mesh

Posted: Sun Mar 31, 2013 3:37 pm
by axtyax
ok here is the code:

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;

int main() {
IrrlichtDevice* device;

createDevice(EDT_OPENGL,dimension2d<u32>(640,480),16, false,false,false,0);

if (!device) return 1;

IVideoDriver* driver;
device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IMesh* mesh = smgr->getMesh("sydney.md2");
IMeshSceneNode* node = smgr->addMeshSceneNode(mesh);
smgr ->addCameraSceneNode(0,vector3df(0,30,-40),vector3df(0,5,0));


while (device->run()) {

driver->beginScene(true,true,SColor(255,255,255,255));

smgr->drawAll();

driver->endScene();
}

device->drop();
return 0;
}

this was built with code::blocks on linux mint cinnamon, and when run it says: Segmentation fault (core dumped).

Re: Segmentation fault when loading a mesh

Posted: Sun Mar 31, 2013 5:49 pm
by Mux
Without running the code I see that this won't work:

Code: Select all

IrrlichtDevice* device;
createDevice(EDT_OPENGL,dimension2d<u32>(640,480),16, false,false,false,0);
 
Do it like this:

Code: Select all

 
IrrlichtDevice* device = createDevice(EDT_OPENGL,dimension2d<u32>(640,480),16, false,false,false,0);
 

Re: Segmentation fault when loading a mesh

Posted: Sun Mar 31, 2013 7:38 pm
by axtyax
i just did this but it wont work for some reason.
Now it says Aborted(core dumped)
could this problem be caused by my distrobution of linux
(im using mint but i got the irrlicht for ubuntu)

Re: Segmentation fault when loading a mesh

Posted: Sun Mar 31, 2013 10:25 pm
by Mel
Dude, do you know what pointers are? You are declaring pointers but never assigning them, hence the crash.

the failure is here, guess why.

Code: Select all

driver->beginScene(true,true,SColor(255,255,255,255));
Unless you know what happens here, you can't really use Irrlicht, or else you will be finding yourself inmerse in continuous, and elemental errors, and you won't know why. Keep studying.

Re: Segmentation fault when loading a mesh

Posted: Sun Mar 31, 2013 11:25 pm
by axtyax
yes i do.
i use to use the Alegro engine for 2d graphics in c++
when loading a bitmaps i would get this error very often.

i wld really like to know why exactly, but i think it is to do with the program loading up too much data, and getting rid of it
in order to avoid a crash.

Re: Segmentation fault when loading a mesh

Posted: Mon Apr 01, 2013 8:25 am
by Mel
I wonder why... well, that's not it, a program may load tons of memory and free it many times and won't crash. It is more elemental than that. Take a look at the pointers you are declaring. There is something missing that is very important to pointers, to any variable actually, but for pointers this lack leads to program errors and core dumpings.

Re: Segmentation fault when loading a mesh

Posted: Mon Apr 01, 2013 9:31 am
by eejin

Code: Select all

IVideoDriver* driver;
device->getVideoDriver();
Take a good look at it.

Re: Segmentation fault when loading a mesh

Posted: Mon Apr 01, 2013 2:04 pm
by axtyax
well, pointers point to any, say variable, by being equal to that variables address in the computers data storage.
So is it because the driver isnt being assigned anything to point to???

Re: Segmentation fault when loading a mesh

Posted: Mon Apr 01, 2013 2:11 pm
by axtyax
and also the first line of eejin's copied code is declaring the driver, creating it.
but i dont understand why then the device is '"getting" it again?

Re: Segmentation fault when loading a mesh

Posted: Mon Apr 01, 2013 3:00 pm
by eejin
It is from your post. You don't give driver a correct value. Thus you should give it the value that getVideoDriver returns.

Code: Select all

IVideoDriver* driver = device->getVideoDriver();
Now driver points to the videodriver. Now beginscene can be called with the correct driver :D.

Re: Segmentation fault when loading a mesh

Posted: Mon Apr 01, 2013 3:33 pm
by axtyax
im gonna try it.
4 some reason when i looked in my book, my code was right.
maybe because the book is for irrlicht 1.7.2.
i case u wanna take a look it's called irrlicht 1.7.2 a begginers guide

Re: Segmentation fault when loading a mesh

Posted: Mon Apr 01, 2013 3:34 pm
by axtyax
wait it still says aborted(core dumped)????

Re: Segmentation fault when loading a mesh

Posted: Mon Apr 01, 2013 3:48 pm
by axtyax
i think that this is to do with my code not being able to access sydney.md2 and is giving me the error.
i know this because i used to use allegro for 2d graphics and this was the most common problem,
the program wouldnt be able to load the bitmaps.

Re: Segmentation fault when loading a mesh

Posted: Mon Apr 01, 2013 5:56 pm
by eejin
I looked over it but shouldn't this:

Code: Select all

IrrlichtDevice* device;
createDevice(EDT_OPENGL,dimension2d<u32>(640,480),16, false,false,false,0);
Be this:

Code: Select all

IrrlichtDevice* device = createDevice(EDT_OPENGL,dimension2d<u32>(640,480),16, false,false,false,0);

Re: Segmentation fault when loading a mesh

Posted: Mon Apr 01, 2013 11:07 pm
by Mel
Yes, pointers are initialized using an assignation, hence, anything like

IVideoDriver* driver;
device->getVideoDriver();

is not correct because driver is never assigned.

IVideoDriver* driver = device->getVideoDriver();

And it is the same as with all the pointers.

Now, Not being able to load the sydney.md2 model is a more reasonable error. You probably haven't suplied it the right path. But Irrlicht returns 0 when it is unable to load a mesh, so you should check that the returned value of the loading method isn't 0; in that case, keep going on, else, you won't see a thing, but the program won't crash.

Try executing your program within the debugger, it will tell you the exact line the program is failing, and what the error is. a Segmentation fault and core dumping is due mainly to bad pointers usage. This works with Allegro too.