Page 1 of 1

Program crashes when I use an EventReceiver

Posted: Mon Feb 15, 2010 9:10 pm
by nathanf534
When I add an event receiver to my code, it crashes (its the MastEventReceiver class posted on our forums)

Code: Select all

int main(){
	cout<<"starting"<<endl;
	EventReceiver* er;
	cout<<er<<endl;
	IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2d<u32>(800,500), 32,false, false, true,er);//if I set the event receiver here, it crashes on this line

	//device->setEventReceiver(er);//if I set the event receiver here, it crashes at the indicated line below
	cout<<"end init"<<endl;
	NewtonWorld *nWorld = NewtonCreate();
	NewtonSetSolverModel(nWorld,0);
	device->setWindowCaption(L"Basic Collision Detection with Newton GD");
	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	SObject sphere;
	SObject cube;
	NewtonCube* nCube;
	nCube->mesh = smgr->getMesh("C:/Models/testCube.b3d");//CRASHES HERE
	nCube->node = smgr->addMeshSceneNode(nCube->mesh);
	nCube->node->setPosition(vector3df(0,3,0));
	nCube->node->getMaterial(0).EmissiveColor.set(255,255,255,255);
	nCube->collision = NewtonCreateBox(nWorld, 2, 2, 2, NULL,NULL);
	nCube->gravity=-4;
	nCube->body = NewtonCreateBody(nWorld, nCube->collision);
	matrix4 mat;
	mat.setTranslation(vector3df(0,5,0));
	NewtonBodySetMatrix(nCube->body, &mat.M[0]);
	NewtonBodySetUserData(nCube->body,nCube);
	NewtonBodySetMassMatrix(nCube->body,10,150,150,150);
	NewtonBodySetTransformCallback(nCube->body,callbackTransform);
	NewtonBodySetForceAndTorqueCallback(nCube->body, ApplyForceAndTorqueEvent);

Re: Program crashes when I use an EventReceiver

Posted: Mon Feb 15, 2010 9:16 pm
by Acki
EventReceiver* er;
this creates an undefined pointer (to garbage) !!! :roll:

try this:

Code: Select all

EventReceiver er;
   IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2d<u32>(800,500), 32,false, false, true, &er);
but this won't work too, unless you create your own receiver class !!! :roll:
the tutorials are showing this pretty well !!! :lol:

Posted: Mon Feb 15, 2010 9:29 pm
by nathanf534
I changed the code you said, but it still does the same thing. Also, what do you mean by it won't work unless I create my own receiver class? I'm using the MastEventReceiver (just renamed it to EventReceiver).

Posted: Mon Feb 15, 2010 9:42 pm
by Acki
then tell us where it crashes, how it crashes and the error message you got...

Posted: Mon Feb 15, 2010 9:45 pm
by nathanf534
I put where it crashes in the comments of the code. When it crashes, I just get the windows error message "IrrlichtGame.exe has stopped working", and it exits.

Posted: Mon Feb 15, 2010 9:53 pm
by Acki
you're using this receiver: http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=21657 ???
creating and passing it to the device like I described works like expected, but you'll have to update the onEvent function declaration, because it's out of date... ;)
also if you don't change the onEvent declaration it should not compile !!! :shock:

Posted: Mon Feb 15, 2010 9:59 pm
by nathanf534
yea, I updated the OnEvent method, and it compiles fine. It just crashes while it's running.

Posted: Mon Feb 15, 2010 10:27 pm
by Acki
for me it runns like a charm !!!
I bet the error is somewhere else in your code..
also this tells me that it's not the receiver himselve:

Code: Select all

//device->setEventReceiver(er);//if I set the event receiver here, it crashes at the indicated line below 
...
nCube->mesh = smgr->getMesh("C:/Models/testCube.b3d");//CRASHES HERE 

Posted: Mon Feb 15, 2010 10:27 pm
by nathanf534
hmm... I changed

Code: Select all

NewtonCube *nCube;
to

Code: Select all

NewtonCube nCube;
(and changed other parts of the code to make that work) and it seems to be working now. I guess that just didn't show up until I tried to add an eventReceiver... wierd :roll:

Thanks for helping me :D

Posted: Mon Feb 15, 2010 10:30 pm
by Acki
aha, of course the same like with the receiver

Code: Select all

   NewtonCube* nCube;
this creates an undefined pointer (to garbage), too !!! :lol:
maybe take a deeper look on pointers... :roll:

Posted: Mon Feb 15, 2010 10:35 pm
by nathanf534
I always thought

Code: Select all

Object name;
and

Code: Select all

Object* name;
were the exact same except that the 2nd returns a pointer to the object... but I guess I'm wrong. Ill go look that up.

Posted: Mon Feb 15, 2010 10:53 pm
by Zeus
you realy should learn something about pointers and references ...

http://www.cplusplus.com/doc/tutorial/pointers/
http://www.augustcouncil.com/~tgibson/tutorial/ptr.html

your object reference has to exists if you want to use it by pointers cause the pointer himself is no valid object ...

regards zeus

Posted: Mon Feb 15, 2010 11:23 pm
by nathanf534
Thanks, those explained a lot to me.

Posted: Tue Feb 16, 2010 10:06 am
by polylux
The base line is:

Code: Select all

Object *name;
creates a pointer that's literally pointing to nowhere and - more importantly for you - DOES NOT create an object along with it.
What you can do now is let it point to (assign) an existing object or you create an object of type "Object" explicitly with the "new" operator:

Code: Select all

name = new Object();
Don't forget that you'll have to destroy (delete) dynamically allocated objects when you don't need them anymore:

Code: Select all

delete name;
Hope this helps a bit.

Cheers,
p.