PxPhysics object lost after initialization

Hello!

I just downloaded PhysX3.3 and for some reason my main physics object becomes null as soon as initialization code is finished.

Definition:

namespace g {
	
	
	// PhysX

	static PxPhysics*				pPhysXSDK = NULL;

	PxFoundation*					pPhysXFoundation;
	PxCooking*						pPhysXCooking;
	PxScene*						pPhysXScene;
	PxMaterial*						pPxMaterial;
};

Initialization:

bool InitializePhysX()
{
	static PxDefaultErrorCallback gDefaultErrorCallback;
	static PxDefaultAllocator gDefaultAllocatorCallback;
	static PxSimulationFilterShader gDefaultSFS = PxDefaultSimulationFilterShader;

	g::pPhysXFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback);

	if(!g::pPhysXFoundation)
	{
		MessageBox(0, "PxCreateFoundation() failed", "Error", MB_ICONERROR);
		return false;
	}
	g::pPhysXSDK = PxCreatePhysics(PX_PHYSICS_VERSION, *g::pPhysXFoundation, PxTolerancesScale());

	if(!g::pPhysXSDK)
	{
		MessageBox(0, "PxCreatePhysics() failed", "Error", MB_ICONERROR);
		return false;
	}

	if(!PxInitExtensions(*g::pPhysXSDK))
	{
		MessageBox(0, "PxInitExtensions() failed", "Error", MB_ICONERROR);
		return false;
	}

	PxSceneDesc psd(g::pPhysXSDK->getTolerancesScale());
	psd.gravity = PxVec3(0.0f, -9.81f, 0.0f);
	if(!psd.cpuDispatcher)
	{
		PxDefaultCpuDispatcher* cpuDispatcher = PxDefaultCpuDispatcherCreate(1);
		if(!cpuDispatcher)
		{
			MessageBox(0, "PxDefaultCpuDispatcherCreate() failed", "Error", MB_ICONERROR);
			return false;
		}
		psd.cpuDispatcher = cpuDispatcher;
	}
	if(!psd.filterShader)
	{
		psd.filterShader = gDefaultSFS;
	}

	g::pPhysXScene = g::pPhysXSDK->createScene(psd);
	if(!g::pPhysXScene)
	{
		MessageBox(0, "PhysXSDK->createScene() failed", "Error", MB_ICONERROR);
		return false;
	}

	g::pPxMaterial = g::pPhysXSDK->createMaterial(0.5, 0.5, 0.5);

	g::pPhysXCooking = PxCreateCooking(PX_PHYSICS_VERSION, *g::pPhysXFoundation,
		PxCookingParams(g::pPhysXSDK->getTolerancesScale()));
	if(!g::pPhysXCooking)
	{
		MessageBox(0, "PxCreateCooking() failed", "Error", MB_ICONERROR);
		return false;
	}
	return true;
}

Initialization returns true, but as soon as I try to do anything that involves calling the main PxPhysics object (g::pPhysXSDK) - like: creating material, the application crashes with unhandled exception error because g::pPhysXSDK is 0.

So I can use g::pPhysXSDK only inside InitializePhysX(). Meaning I can’t do anything at runtime… I don’t think it’s supposed to be this way.

Ignore. Silly mistake.

By the way. I am getting some memory leaks from InitializePhysX(). Does anyone know why?

This is my cleanup method:

void ReleasePhysX()
{
	PxCloseExtensions();
	if(g::pPxMaterial) g::pPxMaterial->release();
	if(g::pPhysXCooking) g::pPhysXCooking->release();
	if(g::pPhysXScene) g::pPhysXScene->release();
	if(g::pPhysXSDK) g::pPhysXSDK->release();
	if(g::pPhysXFoundation) g::pPhysXFoundation->release();
}

It looks like you still need to release the cpuDispatcher.

Hope that works out,

Gordon

Yes. That did it. Thank you.