Unhandled exception when creating physics

Hi,
I’m trying to develop an application using PhysX 3.2 and when I call PxCreatePhysics, I get an unhandled exception : “Unhandled exception in 0x5D5CD181 (PhysX3Common_x86.dll) in Test.exe: 0xC0000005: Access violation when reading location 0x00000028.” Does anyone know how to fix it ?

Hi,

There are many reasons for it…
Maybe this one: The .dll aren´t the same version as the .lib. ( But that´s surely not the problem )
But it seems you are trying to create the physics with a null pointer as
parameter - it could be the PxAllocator which isn´t created, or the PxErrorCallback inside
the PxFoundation Pointer. You also need to initalize the PxProfilZoneManager.
Just show us your code how you are initialize it.
Its also possible when you are trying to create the PhysX with a wrong PxVersion number.

It would be easier when you say as much information as possible.
Do you have any error messages?

Just debug your code - and you will see the values of the pointer.

Here is my code :

#include "Physics.h"
#include <PxPhysicsAPI.h>
#include <PxDefaultErrorCallback.h>
#include <PxDefaultAllocator.h>
#include <PxExtensionsAPI.h>
#include <foundation\PxFoundation.h>
#include <iostream>

using namespace physx;
using namespace std;

bool Record_Memory_Allocation = true;
PxPhysics *p_Physics = NULL;
PxFoundation *p_Foundation;
PxProfileZoneManager *p_ProfileZoneManeger;
static PxDefaultErrorCallback DefaultErrorCallback;
static PxDefaultAllocator DefaultAllocatorCallback;

void Init_Physics()
{

	p_Foundation = PxCreateFoundation(PX_PHYSICS_VERSION, DefaultAllocatorCallback, DefaultErrorCallback);
	if(!p_Foundation)
	{
		cerr << "PxCreatep_Foundation Failed\n";
		exit(-1);
	}
	p_ProfileZoneManeger = &PxProfileZoneManager::createProfileZoneManager(p_Foundation);
	if(!p_ProfileZoneManeger)
	{
		cerr << "createProfileZoneManager Failed\n";
		exit(-1);
	}
	p_Physics = PxCreatePhysics(PX_PHYSICS_VERSION, *p_Foundation, PxTolerancesScale(), Record_Memory_Allocation, p_ProfileZoneManeger);
	if(!p_Physics)
	{
		cerr << "PxCreatePhysics Failed\n";
		exit(-1);
	}
}

I downloaded the lastest PhysX SDK so I think that the dll are ok.
When I debug, everything seems all right, I can’t say if the PxDefaultAllocator and the PxDefaultErrorCallback are ok because I don’t know exactly how they are supposed to be, but I think they are since I reach the PxCreatePhysics instruction.
The program crashes during the execution of this instruction. According to the debugger, the problem is here :

class PxDefaultAllocator : public PxAllocatorCallback
{
public:
	void* allocate(size_t size, const char*, const char*, int)
	{
		<u><b>return _aligned_malloc(size, 16);</b></u>
	}

	void deallocate(void* ptr)
	{
		_aligned_free(ptr);
	}
};

But I don’t know if it’s right.
The error I get is the one I wrote : “Unhandled exception…”

Hi,

as I said you are trying to use a non initalizied PxDefaultAllocator and PxDefaultErrorCallback.

You also should use a pointer to it - you can delete it when you delete the PhysX in your app.
( One litte mention: You should create a new class which handels the whole PhysX. It seems
you are writing this code in your main source file )

And there is another thing: Set all your pointer always to NULL when you declare it.
( And of course, write a class with a constructor to do the initailization. )

PxAllocatorCallback* DefaultErrorCallback = new PxDefaultAllocator();
PxDefaultErrorCallback* DefaultAllocatorCallback = new PxDefaultErrorCallback();
p_Foundation = PxCreateFoundation(PX_PHYSICS_VERSION, *DefaultAllocatorCallback ,*DefaultErrorCallback);

Now it should work.

Hi,

Thank you for your help.
I did what you said so here is my new code :

#include "Physics.h"

bool Record_Memory_Allocation = true;
PxPhysics *p_Physics = NULL;
PxFoundation *p_Foundation = NULL;
PxProfileZoneManager *p_ProfileZoneManeger = NULL;
PxErrorCallback *DefaultErrorCallback = NULL;
PxAllocatorCallback *DefaultAllocatorCallback = NULL;

void Init_Physics()
{
	DefaultAllocatorCallback = new PxDefaultAllocator();
	if(!DefaultAllocatorCallback)
	{
		cerr << "Unable to create DefaultAllocatorCallback\n";
		exit(-1);
	}
	DefaultErrorCallback = new PxDefaultErrorCallback();
	if(!DefaultErrorCallback)
	{
		cerr << "Unable to create DefaultErrorCallback\n";
		exit(-1);
	}
	
	p_Foundation = PxCreateFoundation(PX_PHYSICS_VERSION, *DefaultAllocatorCallback, *DefaultErrorCallback);
	if(!p_Foundation)
	{
		cerr << "PxCreatep_Foundation Failed\n";
		exit(-1);
	}
	p_ProfileZoneManeger = &PxProfileZoneManager::createProfileZoneManager(p_Foundation);
	if(!p_ProfileZoneManeger)
	{
		cerr << "createProfileZoneManager Failed\n";
		exit(-1);
	}
	
	p_Physics = PxCreatePhysics(PX_PHYSICS_VERSION, *p_Foundation, PxTolerancesScale(), Record_Memory_Allocation, p_ProfileZoneManeger);
	if(!p_Physics)
	{
		cerr << "PxCreatePhysics Failed\n";
		exit(-1);
	}
	
}

But I get the same error. The program crashes when the function PxCreatePhysics calls PxCreateBasePhysics.

I haven’t made a class yet but this is the next step. This code isn’t in my main file, for now, it’s just a function but I will correct that.

Hi,
which version of PhysX do you use? For Windows - or for linux and which lib do you use -
DEBUG , CHECKED or the release ( x86 or x64 ) ?

Im not currently on my pc - so I cant test it.
When I´m home I will test your code. ( Maybe at 8.30 pm )
Its not possible for me to debug the PxCreatePhysics function, because I dont have the source code - or an computer here.

Update: I´ve had a few minutes to test your code - it works well.

So there is another error.

Hi,
I use PhysX 3.2.0 for Windows.

Update: I was using the CHECKED lib. I just swiched to the release ones and now it seems to work.
Thank you very much for your help.

Hi,

thats great news.
But you probably should use PhysX 3.2.3 - which have many bug fixes.

  • And the CHECKED libs are important for debugging - you should use these ( or DEBUG libs )
    for coding and testing instead of release ones, because they have more checks / tests against invalid
    parameter / pointer.