Debug Assertion Error

Hi,

I am having trouble using PhysX in Debug mode. I’m a PhysX noob, so maybe I am doing something wrong. I compiled my dll and libs and I could linked them into my program, but when I run my application in debug mode, I get a Debug Assertion Error on line 74 in PsFoundation.cpp with the message “PhysX3CommonDEBUG assertion failed: mInstance”. When I run my app in release mode everything works fine. I did not code something awesome, I just followed the snipplet HelloWorld implementention.
Do I need something more than just the .lib and .dll files like .ilk, .map or .exp?

Best regards
AlK

This assertion is occurring because the Foundation singleton has not been constructed. I think to fix this you want to call PxCreateFoundation (if you’re already calling it then call it sooner). The helloworld snippet does this in initPhysics.

The reason you’re not hitting the assertion in release is that the PX_ASSERT macro only throws when compiled with the debug flag. However if this assertion would fail then you’re going to get unpredictable behavior (crashes) even in release.

That cannot be the solution, 'cause I call PxCreateFoundation right at the beginning of my main function.
My main() looks like this:

PxFoundation* gFoundation = NULL;
PxPhysics* gPhysics = NULL;
PxDefaultCpuDispatcher * gDispatcher = NULL;
PxScene* gScene = NULL;

int main() {
PxDefaultErrorCallback gDefaultErrorCallback;
PxDefaultAllocator gDefaultAllocatorCallback;

gFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback);

//check if gFoundation is NULL → is not

PxProfileZoneManager* pZoneManager = &PxProfileZoneManager::createProfileZoneManager(gFoundation);
gPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *gFoundation, PxTolerancesScale(), true, pZoneManager);
// check if gPhysics is NULL → is not

PxSceneDesc sceneDesc(gPhysics->getTolerancesScale());
sceneDesc.gravity = PxVec3(0.0f, -9.8f, 0.0f);
gDispatcher = PxDefaultCpuDispatcherCreate(0); // ASSERTION ERROR


}

It doesnt matter what argument PxDefaultCpuDispatcherCreate gets, it always throws an assertion error.

As far as I can tell, if PxCreateFoundation returns a value other than null then that means mInstance has to be non-null. The only explanation I can see for it subsequently being null (aside from memory errors etc) would be if something later set it to null. To test this you could call PxGetFoundation() on each line after PxCreateFoundation, which executes that assert and see if and when it starts failing.

I can’t see anything wrong with what you’re doing.

So, I did what you suggested, but PxGetFoundation() always returns something. I can’t tell if it’s valid, as it returns a reference. The content of that reference is never empty or something like that.
I was also looking into the PsFoundation code and I saw, that on line 74 “PX_ASSERT(mInstance)” is called and in the same file on line 80 “PX_ASSERT(mInstance != NULL)” is called. Is there any difference between those two asserts or are they checking the same thing?
And my includes look like this:

#include “physx\PxPhysicsAPI.h”

#if _DEBUG
#pragma comment(lib, “PhysX3DEBUG_x86.lib”)
#pragma comment(lib, “PhysX3CommonDEBUG_x86.lib”)
#pragma comment(lib, “PhysX3ExtensionsDEBUG.lib”)
#pragma comment(lib, “PhysXProfileSDKDEBUG.lib”)
#else
#pragma comment(lib, “PhysX3_x86.lib”)
#pragma comment(lib, “PhysX3Common_x86.lib”)
#pragma comment(lib, “PhysX3Extensions.lib”)
#pragma comment(lib, “PhysXProfileSDK.lib”)
#endif

PX_ASSERT(exp) only ever uses exp in the expression !!(exp). If mInstance = null then !!(mInstance) = !true = false and !!(mInstance != NULL) = !!(false) = false. If mInstance != null then !!(mInstance) = !false = true and !!(mInstance != NULL) = !!true = true.

So the expressions PX_ASSERT(mInstance) and PX_ASSERT(mInstance != NULL) are equivalent.

Maybe it’s time to attach a debugger and see what happens between the last time you call PxGetFoundation() and the point at which the assertion fails.

I know it may be late to the game here, but since 64 bit implementations of PhysX require 16 byte alignment, make sure you’re declaring #define WIN32 for your 32 bit applications, and the allocate functions wont stop at 16 byte alignment. Here’s the code from PxDefaultAllocator.h to back this up:

void* allocate(size_t size, const char*, const char*, int)
{

#if defined(WIN32)
// on win32 we only have 8-byte alignment guaranteed, but the CRT provides special aligned allocation
return _aligned_malloc(size, 16);
#elif defined (ANDROID)
void *ptr = memalign(16, size);
PX_ASSERT((reinterpret_cast<size_t>(ptr) & 15)==0);
return ptr;
#else
// on PS3, XBox and Win64 we get 16-byte alignment by default
void *ptr = ::malloc(size);
PX_ASSERT((reinterpret_cast<size_t>(ptr) & 15)==0);
return ptr;
#endif
}