Advertisement

Exception Thrown and Unhandled Exception

Started by February 29, 2020 11:27 PM
12 comments, last by JoeJ 4Β years, 7Β months ago

Hello all,

I am trying to get this code to work. It builds fine, but when running it I get exception thrown: 0x001B656F in IrrBullet.exe: 0xC0000005: Access violation reading location 0x0000001C. Then I get Unhandled exception at 0x001B656F in IrrBullet.exe: 0xC0000005: Access violation reading location 0x0000001C.

I am using C++ and Irrlicht 3D engine and Bullet physics. I have posted the code from where the exception is thrown. I can post more code if it is needed.

	u32 TimeStamp = Timer->getTime(), DeltaTime = 0; //update time and physics
	while (!Done)
	{
		DeltaTime = Timer->getTime() - TimeStamp;
		TimeStamp = Timer->getTime();

		UpdatePhysics(DeltaTime);
	}
static void UpdatePhysics(u32 TDeltaTime)
{
	World->stepSimulation(TDeltaTime * 0.001f, 60); //exception comes for this line

	for (list<btRigidBody*>::Iterator it = Bodies.begin(); it != Bodies.end(); ++it)
	{
		UpdateGraphics(*it);
	}
}

u32 is an unsigned int. You need floating point.

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

Advertisement

fleabay said:

u32 is an unsigned int. You need floating point.

I still get the same error after changing it to f32.

LeftyGuitar said:
it

all of them

There may be other issues. I just posted what I noticed.

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

Other than what fleabay pointed out, the code you posted seems ok. But the problem is probably elsewhere.

Step through the code with a debugger and figure out where the code is doing something you don't expect.

Well I tried running through it with a debugger, but it still stops at the stepsimulation function. I can post all my code and see if maybe I'm doing something that is getting overlooked.

#include <iostream>

#include <irrlicht.h>
#include <btBulletDynamicsCommon.h>
#include <btBulletCollisionCommon.h>

using namespace std;

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

static void CreateInitialScene();
static void CreateGround(const btVector3& TPosition, const vector3df& TScale, btScalar TMass);
static void CreateBox(const btVector3& TPosition, const vector3df& TScale, btScalar TMass);
static void CreateBall(const btVector3& TPosition, btScalar TRadius, btScalar TMass);
static void UpdatePhysics(f32 TDeltaTime);
static void UpdateGraphics(btRigidBody* TBodies);
static void ClearBodies();

static int GetRandInt(int TMax) { return rand() % TMax; }

static bool Done = false;
static btDiscreteDynamicsWorld* World;
IrrlichtDevice* Device;
IVideoDriver* Driver;
ISceneManager* Smgr;
IGUIEnvironment* Env;
IFileSystem* File;
ITimer* Timer;
ILogger* Log;

list <btRigidBody*> Bodies;

class MainEvent : public IEventReceiver {

public:

	virtual bool OnEvent(const SEvent &TEvent) {

		if (TEvent.EventType == EET_KEY_INPUT_EVENT && !TEvent.KeyInput.PressedDown) {
			switch (TEvent.KeyInput.Key) {
			case KEY_ESCAPE:
				Done = true;
				break;
			case KEY_KEY_1:
				CreateBox(btVector3(GetRandInt(10) - 5.0f, 7.0f, GetRandInt(10) - 5.0f), vector3df(GetRandInt(3) + 0.5f, GetRandInt(3) + 0.5f, GetRandInt(3) + 0.5f), 1.0f);
				break;
			case KEY_KEY_2:
				CreateBall(btVector3(GetRandInt(10) - 5.0f, 7.0f, GetRandInt(10) - 5.0f), GetRandInt(5) / 5.0f + 0.2f, 1.0f);
				break;
			case KEY_KEY_X:
				CreateInitialScene();
				break;
			default:
				return false;
				break;
			}

			return true;
		}

		return false;
	}
};

int main(int argc, char* argv[])
{
	MainEvent receiver;

	Device = createDevice(EDT_OPENGL, dimension2d<u32>(1024, 720), 32, false, false, false, &receiver);
	Env = Device->getGUIEnvironment();
	Timer = Device->getTimer();
	Smgr = Device->getSceneManager();
	Driver = Device->getVideoDriver();

	btDefaultCollisionConfiguration* collisionCong = new btDefaultCollisionConfiguration();
	btBroadphaseInterface* broadphase = new bt32BitAxisSweep3(btVector3(-1000, -1000, -1000), btVector3(1000, 1000, 1000));
	btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionCong);
	btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver();
	World = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionCong);

	ICameraSceneNode* Cam = Smgr->addCameraSceneNode();
	Cam->setPosition(vector3df(0.0f, 5.0f, -15.0f));
	Cam->setTarget(vector3df(0, 0, 0));

	Smgr->addLightSceneNode(0, vector3df(2, 5, -2), SColorf(4, 4, 4, 1));
	CreateInitialScene();

	f32 TimeStamp = Timer->getTime(), DeltaTime = 0;
	while (!Done)
	{
		DeltaTime = Timer->getTime() - TimeStamp;
		TimeStamp = Timer->getTime();

		UpdatePhysics(DeltaTime);

		Driver->beginScene(true, true, SColor(255, 20, 0, 0));
		Smgr->drawAll();
		Env->drawAll();
		Driver->endScene();
		Device->run();
	}

	ClearBodies();

	delete World;
	delete solver;
	delete dispatcher;
	delete broadphase;
	delete collisionCong;

	Device->drop();

	return 0;
}

static void UpdatePhysics(f32 TDeltaTime)
{
	World->stepSimulation(TDeltaTime * 0.001f, 60);

	for (list<btRigidBody*>::Iterator it = Bodies.begin(); it != Bodies.end(); ++it)
	{
		UpdateGraphics(*it);
	}
}

static void CreateInitialScene()
{
	ClearBodies();
	CreateBox(btVector3(0.0f, 0.0f, 0.0f), vector3df(10.0f, 0.5f, 10.0f), 0.0f);
}

static void CreateGround(const btVector3& TPosition, const vector3df& TScale, btScalar TMass)
{

}

static void CreateBox(const btVector3& TPosition, const vector3df& TScale, btScalar TMass)
{
	ISceneNode* Node = Smgr->addCubeSceneNode(1.0f);
	Node->setScale(TScale);
	Node->setMaterialFlag(EMF_LIGHTING, 1);
	Node->setMaterialFlag(EMF_NORMALIZE_NORMALS, true);

	btTransform Transform;
	Transform.setIdentity();
	Transform.setOrigin(TPosition);

	btDefaultMotionState* Motion = new btDefaultMotionState(Transform);

	btVector3 HalfExtents(TScale.X * 0.5f, TScale.Y * 0.5f, TScale.Z * 0.5f);
	btCollisionShape* Shape = new btBoxShape(HalfExtents);

	btVector3 LocalInertia;
	Shape->calculateLocalInertia(TMass, LocalInertia);

	btRigidBody* Body = new btRigidBody(TMass, Motion, Shape, LocalInertia);

	Body->setUserPointer((void*)(Node));

	World->addRigidBody(Body);
	Bodies.push_back(Body);
}

static void CreateBall(const btVector3& TPosition, btScalar TRadius, btScalar TMass)
{

}

static void UpdateGraphics(btRigidBody* TBodies)
{
	ISceneNode* Node = static_cast<ISceneNode*>(TBodies->getUserPointer());

	btVector3 Point = TBodies->getCenterOfMassPosition();
	Node->setPosition(vector3df((f32)Point[0], (f32)Point[1], (f32)Point[2]));

	vector3df Euler;
	const btQuaternion& TQuat = TBodies->getOrientation();
	quaternion q(TQuat.getX(), TQuat.getY(), TQuat.getZ(), TQuat.getW());
	q.toEuler(Euler);
	Euler *= RADTODEG;
	Node->setRotation(Euler);
}

static void ClearBodies()
{
	for (list<btRigidBody *>::Iterator It = Bodies.begin(); It != Bodies.end(); ++It) {
		btRigidBody *Object = *It;

		// Delete irrlicht node
		ISceneNode *Node = static_cast<ISceneNode *>(Object->getUserPointer());
		Node->remove();

		// Remove the object from the world
		World->removeRigidBody(Object);

		// Free memory
		delete Object->getMotionState();
		delete Object->getCollisionShape();
		delete Object;
	}

	Bodies.clear();
}
Advertisement

http://forums.codeblocks.org/index.php?topic=18318.0

If you think you're going to cut and paste your way into becoming a programmer (from square one no doubt) then You've Got Another Thing Coming. Not AC/DC but close enough.

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

An access violation at a location very close to zero usually means you accessed a NULL pointer somewhere (or you passed NULL somewhere where you shouldn't).

Run with a debugger, wait for the exception to happen, look at the stack trace and you should find your NULL object.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

fleabay said:

http://forums.codeblocks.org/index.php?topic=18318.0

If you think you're going to cut and paste your way into becoming a programmer (from square one no doubt) then You've Got Another Thing Coming. Not AC/DC but close enough.

Yes I am using a tutorial to help me figure out how to use Bullet with Irrlicht. I did change a couple things. This was just to help me get going then modify it as I got it working.

EDIT: Ok After running it through the debugger, I'm getting that the β€œWorld” variable is 0x00000 NULL.

LeftyGuitar said:
Yes I am using a tutorial to help me figure out how to use Bullet with Irrlicht.

You need to understand C++, Bullet, and Irrlicht before you β€˜figure out how to use Bullet with Irrlicht.’

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

This topic is closed to new replies.

Advertisement