Confusing crash

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
DawsonXB360
Posts: 13
Joined: Fri Mar 18, 2011 7:55 pm
Location: 219

Confusing crash

Post by DawsonXB360 »

Hey I was wondering if any c++ wizards out there could help me with this confusing crash.

Code: Select all

			case 8:
				if (event.GUIEvent.EventType == EGET_SCROLL_BAR_CHANGED)
				{
					game->returnGUIMenus()->ccScreen->updateBodyPart(game->returnGUIMenus()->selectedBP);
				}
			break;
What this does is if the scroll bar is changed it calls a function updateBodyPart() and gives it the number of a selected item in a list box

Code: Select all

void CCScreen::updateBodyPart(int n)
{

	int shape = game->returnGUIMenus()->shapeBar->getPos();
	int scaleX = game->returnGUIMenus()->scaleXBar->getPos();
	int scaleY = game->returnGUIMenus()->scaleYBar->getPos();
	int scaleZ = game->returnGUIMenus()->scaleZBar->getPos();
	int mass = game->returnGUIMenus()->massBar->getPos();

	parts.at(n)->bPart->updateBody(shape,vector3df(scaleX,scaleY,scaleZ));

	parts.at(n)->shape = shape;
	parts.at(n)->scalex = scaleX;
	parts.at(n)->scaley = scaleY;
	parts.at(n)->scalez = scaleZ;
	parts.at(n)->mass = mass;
}
Heres is the updateBodyPart() function. "parts" is a vector of structs the structs contain 5 integers and an object pointer. This function updates the struct with values from scrollbars. My problem is with the updateBody function that is called through the pointer in the struct.

Code: Select all

void BodyPart::updateBody(int shape, vector3df scale)
{

	// Update the irrlicht node
	vector3df pos = Node->getPosition();

	Node->remove();

	if (shape==1)
		Node = device->getSceneManager()->addCubeSceneNode(1.f);
	if (shape==2)
		Node = device->getSceneManager()->addSphereSceneNode(1.f);

	Node->setScale(scale);
	Node->setPosition(pos);
	Node->setMaterialFlag(EMF_LIGHTING, false);
	Node->setMaterialFlag(EMF_NORMALIZE_NORMALS, true);

	// Update physics body
	body->remove();

	ICollisionShape *shapenew;
	if (shape==1)
	{
		shapenew = new IBoxShape(Node, 0, false);
	}
	if (shape==2)
	{
		shapenew = new ISphereShape(Node, 0, false);
	}

	body = physicsworld->ReturnWorld()->addRigidBody(shapenew,1,2);
}

This compiles just fine but as soon as I change a scroll bar it crashes. I'm using VC++ 2008. Here is a picture of what's going on when it crashes.

http://4.bp.blogspot.com/-CPyg6EaHqJI/T ... /error.JPG

even if I comment that line the crash still happens at anywhere BodyPart:: occurs. It's like the pointer in the parts struct doesn't point to a valid object but it should do, i've checked to make sure all the correct values are being passed around. Help will be greatly appreciated this is so annoying!

Thanks in advance.

EDIT:

Code: Select all

void CCScreen::addBodyPart()
{
	vector3df position;
	noParts++;
	parts.resize(noParts);
	parts.at(noParts-1) = new part;

	parts.at(noParts-1)->bPart = new BodyPart;

	if (noParts==1)
		position = vector3df(spawnGlobal+vector3df(499400,200,0));
	if (noParts==2)
		position = vector3df(spawnGlobal+vector3df(499400,50,400));

	parts.at(noParts-1)->bPart->init(game->returnEngine()->ReturnDevice(),game->returnPhysics(), game->returnObjects()->returnWorld(),1,vector3df(100,100,100), 0, position);
	parts.at(noParts-1)->shape = 1;
	parts.at(noParts-1)->scalex = 100;
	parts.at(noParts-1)->scaley = 100;
	parts.at(noParts-1)->scalez = 100;
	parts.at(noParts-1)->mass = 0;
}
Here is the code for adding a body part, just to show that the pointer in the struct should point to a valid object. Also the node for the object appears in game so it should be working.

EDIT:
Replaced with cleaner code.
Last edited by DawsonXB360 on Sun Apr 03, 2011 3:40 pm, edited 1 time in total.
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

This code is pretty messy, how long have you been using C++?

You're constantly referencing to classes instead of objects inside your method bodies, you should never need to do this unless you want to access a static method or field

I guess this won't directly cause any harm inside methods of the class you're actually referencing (although this is a really bad practice), but it will definitely cause harm in other situations, probably causing your crash

I'm actually pretty confused by your code, do both your MyEventReceiver class and CCScreen class have a member called game, or are you mixing up some things here?
DawsonXB360
Posts: 13
Joined: Fri Mar 18, 2011 7:55 pm
Location: 219

Post by DawsonXB360 »

I've only been coding for a couple of months.

MyEventReceiver and CCScreen both have 'game' which is a pointer to the object Game. Game is the top object and all the other objects are created from it. When an object is created from within Game a pointer is passed to it using 'this'

I've done this so I can access all of my objects, like the physics engine or the irrlicht engine through the Game pointer.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

If you click on "this" in your screenshot (in the autos section) you can see which value Node has.
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