new hkRigidBody(...) ~> Crash, why?

new hkRigidBody(...) ~> Crash, why?

Hi there,

i want to use Havok Physics to create a simple scene with multiple rigid bodies. The main-loop to create the rb's looks like this:

for( int i = 0; i < 100; i++ ) {

hkpRigidBody* rigidBody;
{
// Create a box 1 by 2 by 3
hkVector4 halfExtents; halfExtents.set(0.5f, 1.0f, 1.5f);
hkpBoxShape* boxShape = new hkpBoxShape(halfExtents);

hkpRigidBodyCinfo bodyCinfo;
bodyCinfo.m_shape = boxShape;

// Calculate the mass properties for the shape
const hkReal boxMass = 10.0f;
hkpMassProperties massProperties;
hkpInertiaTensorComputer::computeShapeVolumeMassProperties(boxShape, boxMass, massProperties);

bodyCinfo.setMassProperties(massProperties);

// Create the rigid body
rigidBody = new hkpRigidBody(bodyCinfo); // crash after 7th body

// No longer need the reference on the boxShape, as the rigidBody now owns it
boxShape->removeReference();
}

// Add the rigidBody to the world
world->addEntity(rigidBody);

// No longer need the ref of rigidBody - as the world now owns it
//rigidBody->removeReference();

}

The code was taken from the step-by-step exsample. In the 8th iteration of the loop the programm crashs and the MS Visual Studio 2010 Debugger show that message:

Unhandled exception at 0x0195029c in Application.exe: 0xC0000005: Access violation reading location 0xccccccd8.

Call Stack:

Application.exe!hkLargeBlockAllocator::blockAlloc(int bytes) Line 735 + 0x11 bytes C++
Application.exe!hkMemoryAllocator::bufAlloc(int & reqBytesInOut) Line 25 C++
Application.exe!hkFreeList::addSpace() Line 703 C++
Application.exe!hkFreeListAllocator::blockAllocBatch(void * * blocksOut, int nblocks, int nbytes) Line 616 + 0x84 bytes C++
Application.exe!hkThreadMemory::onRowEmpty(int rowIndex) Line 165 C++
Application.exe!hkThreadMemory::blockAlloc(int nbytes) Line 241 C++
Application.exe!hkpSimpleContactConstraintAtomUtil::allocateAtom(int numReservedContactPoints, int numExtraUserDatasA, int numExtraUserDatasB, int maxNumContactPoints) Line 86 C++
Application.exe!hkpSimpleContactConstraintData::hkpSimpleContactConstraintData(hkpConstraintInstance * constraint, hkpRigidBody * bodyA, hkpRigidBody * bodyB) Line 128 C++
Application.exe!hkpSimpleConstraintContactMgr::hkpSimpleConstraintContactMgr(hkpWorld * sm, hkpRigidBody * bodyA, hkpRigidBody * bodyB) Line 68 + 0x53 bytes C++
Application.exe!hkpSimpleConstraintContactMgr::Factory::createContactMgr(const hkpCollidable & a, const hkpCollidable & b, const hkpCollisionInput & input) Line 655 + 0x63 bytes C++
Application.exe!hkpWorldAgentUtil::addAgent(hkpLinkedCollidable * collA, hkpLinkedCollidable * collB, const hkpProcessCollisionInput & input) Line 110 C++
Application.exe!hkpEntityEntityBroadPhaseListener::addCollisionPair(hkpTypedBroadPhaseHandlePair & pair) Line 57 + 0xe bytes C++
Application.exe!hkpTypedBroadPhaseDispatcher::addPairs(hkpTypedBroadPhaseHandlePair * newPairs, int numNewPairs, const hkpCollidableCollidableFilter * filter) Line 52 C++
Application.exe!hkpWorldOperationUtil::addEntityBP(hkpWorld * world, hkpEntity * entity) Line 173 C++
Application.exe!hkpWorld::addEntity(hkpEntity * entity, hkpEntityActivation initialActivationState) Line 891 C++
> Application.exe!Benchmark01_Havok::CreateScene() Line 162 C++
Application.exe!main(int argc, char * * argv) Line 387 + 0x15 bytes C++
Application.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
Application.exe!mainCRTStartup() Line 371 C
kernel32.dll!7735339a()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!77a19ed2()
ntdll.dll!77a19ea5()

The initialization of the Havok Physics Engine is exactly the same as in the example code. I use the "Debug multithreaded" libraries. Do i need to change some compiler/linker options?

Greetings Tobi

7 posts / novo 0
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.

I played around with my code and now I discovered something new. When I paste the code from above right next to the

// Register all collision agents
// It's important to register collision agents before adding any entities to the world.
hkpAgentRegisterUtil::registerAllAgents( world->getCollisionDispatcher() );

no error occure. Do I need to call that statement anytime I want to create new rigid bodies? I encapsuled the whole code in multiple classes the common execution way is approximatly the following:

main() {

1. create class X

X->CreatePhysics(); // inititalize the havok phyiscs

X->CreateScene(); // create rigid bodies

X->Run();

X->ReleaseAll();
}

CreatePhysics and CreateScene are pure virtual functions of a base class and are implemented later by the child classes. The implementation is done over two inheritences.

A (virtual CreatePhysics() = 0, virtual CreateScene() = 0)
^
B (implement CreatePhysics(), virtual CreateScene() = 0)
^
X (implement CreateScene())

Maybe this concept create problems with havok?

[EDIT]

I've create an sample code that is nearly the same as my main project. That code show exactly the same behaviour:

#include 
#include 
#include 
#include 

// Physics
#include 
#include 
#include 
#include 
#include 

// [id=keycode]
#include 

// [id=productfeatures]
// We're using only physics - we undef products even if the keycode is present so
// that we don't get the usual initialization for these products.
#undef HK_FEATURE_PRODUCT_AI
#undef HK_FEATURE_PRODUCT_ANIMATION
#undef HK_FEATURE_PRODUCT_CLOTH
#undef HK_FEATURE_PRODUCT_DESTRUCTION
#undef HK_FEATURE_PRODUCT_BEHAVIOR
//
//// Also we're not using any of these.
//#define HK_EXCLUDE_FEATURE_SerializeDeprecatedPre700
//#define HK_EXCLUDE_FEATURE_RegisterVersionPatches
////#define HK_EXCLUDE_FEATURE_RegisterReflectedClasses
//#define HK_EXCLUDE_FEATURE_MemoryTracker
//#define HK_EXCLUDE_FEATURE_hkpAccurateInertiaTensorComputer
//#define HK_EXCLUDE_FEATURE_CompoundShape
//#define HK_EXCLUDE_FEATURE_hkpAabbTreeWorldManager
//#define HK_EXCLUDE_FEATURE_hkpKdTreeWorldManager
//#define HK_EXCLUDE_FEATURE_TreeBroadPhase
//
#include 
//
//// On Wii we need to provide dummy unit test declarations - deadstripping is not properly done!
//#if defined( HK_COMPILER_MWERKS )
//
//#include 
//hkBool HK_CALL hkTestReport(hkBool32 cond, const char* desc, const char* file, int line)
//{
//	return false;
//}
//#endif

#if defined(HK_PLATFORM_NGP)
unsigned int sceLibcHeapSize = 64*1024*1024;
#endif

static void HK_CALL errorReport(const char* msg, void* userContext)
{
	using namespace std;
	printf("%s", msg);
}

class A {

private:

	hkpWorld *world;

public:

	A(){}
	~A(){}

	void init() {

		hkMallocAllocator baseMalloc;

		// Need to have memory allocated for the solver. Allocate 1mb for it.
		hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault( &baseMalloc, hkMemorySystem::FrameInfo(1024 * 1024) );
		hkBaseSystem::init( memoryRouter, errorReport );

		world = new hkpWorld( hkpWorldCinfo() );

		// Register all collision agents
		// It's important to register collision agents before adding any entities to the world.
		hkpAgentRegisterUtil::registerAllAgents( world->getCollisionDispatcher() );
	}

	void release() {

		// Release the reference on the world
		world->removeReference();

		hkBaseSystem::quit();
		hkMemoryInitUtil::quit();
	}

	void create( ) {

		for( int i = 0; i < 100; i++ ) {

			hkpRigidBody* rigidBody;
			{
				// Create a box 1 by 2 by 3
				hkVector4 halfExtents; halfExtents.set(0.5f, 1.0f, 1.5f);
				hkpBoxShape* boxShape = new hkpBoxShape(halfExtents);

				hkpRigidBodyCinfo bodyCinfo;
				bodyCinfo.m_shape = boxShape;

				//  Calculate the mass properties for the shape
				const hkReal boxMass = 10.0f;
				hkpMassProperties massProperties;
				hkpInertiaTensorComputer::computeShapeVolumeMassProperties(boxShape, boxMass, massProperties);

				bodyCinfo.setMassProperties(massProperties);

				// Create the rigid body
				rigidBody = new hkpRigidBody(bodyCinfo);

				// No longer need the reference on the boxShape, as the rigidBody now owns it
				boxShape->removeReference();
			}
		
			// Add the rigidBody to the world
			this->world->addEntity(rigidBody);

			// No longer need the ref of rigidBody - as the world now owns it
			rigidBody->removeReference();

		}
	}

	void run() {

		// Update as if running at 60 frames per second.
		const hkReal updateFrequency = 60.0f;
		for (int i = 0; i < 20; i++)
		{
			// Do some simulation steps
			this->world->stepDeltaTime(1.0f / updateFrequency);
		}
	}
};

int main( int argc, char **argv ) {

	A *a = new A();
		
	a->init();
	a->create();
	a->run();
	a->release();
	
	delete a;

	return 0;
}

Greetings Tobi

Hey Tobi,
It looks like the problem is with the memory system init. If you do not hold on to

hkMallocAllocator baseMalloc;

for the duration of your program then the memory system will fail to allocate. Which is what's happening here:

Unhandled exception at 0x0195029c in Application.exe: 0xC0000005: Access violation reading location 0xccccccd8.Application.exe!hkLargeBlockAllocator::blockAlloc(int bytes) Line 735 + 0x11 bytes C++

So the simple fix would be to move the declaration of your hkMallocAllocator from the init function into the class that is initializing it (assuming that object sticks around for the duration of the program).

As for

hkpAgentRegisterUtil::registerAllAgents( world->getCollisionDispatcher() );

You only need to register collision agents once, and it must be done before you call world->addEntity().I hope this helps. Let me know if you have any other questions or issues,- Tyler

Hi Tyler,thanks for your quick response. I will check that tomorrow :)Greetings Tobi

Good mornig (@7.14a.m. Germany) :D

I want to say thanks. This simple fix (to move the declaration of "hkMallocAllocator baseMalloc;" into the class itself [member]) solved my problem. All seems to be fine now.

Tyler one more question or please for advice. Maybe you can provide me some simple code snip, which apply a simple 6DOF generic constraint on two rigid bodies.

[EDIT]

+ one more Question:

I've create a trigger (simply conected two cubes by a limited hinge-constraint). When switch that trigger, it smoothly penetrate its limits and is realy springy. Is there a way to make the hinge constraint more stiff?

Greetings Tobi

Hi Tobi,Would you mind making a new thread for each of these new questions?Also what is your use case for the 6dof constraint?As for the limited hinge constraint I would recommend connecting the visual debugger and looking at the constraint viewer to ensure you set up your constraint correctly. Setting up the visual debugger is discussed in the docs under Common Havok Components/Visualization/3. Remote visualization with the Visual Debugger.Cheers,Tyler

Hi,ok i will check that, but I'm sure it's set up correctly. It seems that the force, which should prevent the constraint violating its limits, is to strong.Greetings Tobi

Deixar um comentário

Faça login para adicionar um comentário. Não é membro? Inscreva-se hoje mesmo!