Segmentation fault when loading a mesh

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.
axtyax
Posts: 17
Joined: Sun Mar 31, 2013 3:16 pm

Segmentation fault when loading a mesh

Post 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).
Mux
Posts: 56
Joined: Mon Feb 26, 2007 12:25 pm
Location: Stockholm

Re: Segmentation fault when loading a mesh

Post 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);
 
It'll only take a minute or two to debug this code...
axtyax
Posts: 17
Joined: Sun Mar 31, 2013 3:16 pm

Re: Segmentation fault when loading a mesh

Post 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)
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Segmentation fault when loading a mesh

Post 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.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
axtyax
Posts: 17
Joined: Sun Mar 31, 2013 3:16 pm

Re: Segmentation fault when loading a mesh

Post 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.
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Segmentation fault when loading a mesh

Post 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.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
eejin
Posts: 97
Joined: Sun Jul 24, 2011 11:50 am

Re: Segmentation fault when loading a mesh

Post by eejin »

Code: Select all

IVideoDriver* driver;
device->getVideoDriver();
Take a good look at it.
axtyax
Posts: 17
Joined: Sun Mar 31, 2013 3:16 pm

Re: Segmentation fault when loading a mesh

Post 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???
axtyax
Posts: 17
Joined: Sun Mar 31, 2013 3:16 pm

Re: Segmentation fault when loading a mesh

Post 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?
eejin
Posts: 97
Joined: Sun Jul 24, 2011 11:50 am

Re: Segmentation fault when loading a mesh

Post 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.
axtyax
Posts: 17
Joined: Sun Mar 31, 2013 3:16 pm

Re: Segmentation fault when loading a mesh

Post 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
Last edited by axtyax on Mon Apr 01, 2013 3:38 pm, edited 1 time in total.
axtyax
Posts: 17
Joined: Sun Mar 31, 2013 3:16 pm

Re: Segmentation fault when loading a mesh

Post by axtyax »

wait it still says aborted(core dumped)????
axtyax
Posts: 17
Joined: Sun Mar 31, 2013 3:16 pm

Re: Segmentation fault when loading a mesh

Post 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.
eejin
Posts: 97
Joined: Sun Jul 24, 2011 11:50 am

Re: Segmentation fault when loading a mesh

Post 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);
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Segmentation fault when loading a mesh

Post 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.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Post Reply