HeightField Crash Problem

Dear all

I use HeightField in book sample code environment(book: Learning Physics Modeling with PhysX)
It is ok.

But I use the same code in my Game app environment
4.OS: Win8

When it collide with other, it crash.
no any warning.
First-chance exception at 0x000007F8DFFA775C (PhysX3CHECKED_x64.dll) in APP.exe: 0xC0000005: Access violation writing location 0x000000004E527DA0.
Unhandled exception at 0x000007F8DFFA775C (PhysX3CHECKED_x64.dll) in APP.exe: 0xC0000005: Access violation writing location 0x000000004E527DA0.

How can I debug it?
Thank you very mush.

The code:

PxU32 numRows = 2;
    PxU32 numCols = 2;

    PxHeightFieldSample* samples = (PxHeightFieldSample*)malloc( sizeof(PxHeightFieldSample)*( numRows*numCols ) );
    for ( int count = 0 ; count < 4 ; count++ )
        samples[count].height = (PxI16)0;
        samples[count].materialIndex0 = 1;
        samples[count].materialIndex1 = 1;

    PxHeightFieldDesc hfDesc;
    hfDesc.format       = PxHeightFieldFormat::eS16_TM;
    hfDesc.nbColumns    = numCols;
    hfDesc.nbRows       = numRows;
    hfDesc.samples.data = samples;
    hfDesc.samples.stride = sizeof( PxHeightFieldSample );

    PxHeightField* aHeightField = m_physics->createHeightField( hfDesc );

    PxReal heightScale  = 1.0;      // y
    PxReal rowScale     = 50.0;     // x
    PxReal colScale     = 50.0;     // z

    PxHeightFieldGeometry hfGeom( aHeightField, PxMeshGeometryFlags(), heightScale, rowScale, colScale );

    PxTransform	pos( PxVec3( 0.0f, 0.0f, 0.0f ) );
    PxRigidStatic* aHeightFieldActor    = m_physics->createRigidStatic( pos );
    PxMaterial* material                = m_physics->createMaterial( 0.5f, 0.5f, 0.5f );
    PxShape* aHeightFieldShape = aHeightFieldActor->createShape( hfGeom, *material );

    delete[] samples;
    m_scene->addActor( *aHeightFieldActor );


Why are your material indices 1 when you have only provided a single material? It seems to me that those material indices should be 0, not 1.

In the PhysX sample code, material indices = 1.

and I try 0 or 1 in the sample are both OK.

but I try 0 or 1 in my game app are both crash(When it collide with others).



please use the Debugger for stepping throu your code and see where it crashs.
If you dont know what a Debugger is, or how to use it corretly, just google for it, its very very very very important to know how it works and how you deal with it (Of course, for your IDE).

I didnt test your code, but I see that you use the malloc function. Cant you use new instead of malloc?!

Also, its never a good idea to mix malloc with a delete call - you have to use free for it. (Its an undefined error)

For more infos use google, or look at this first google hit:

Please also check the pointers from PxRigidStatic, PxShape, PxMaterial,samples,
if they are “valid”.

Look into PxHeightFieldDesc ,PxHeightFieldGeometry , PxTransform maybe it has a isValid() function, which should return true if its valid.

But dont forget to step throu your code with the debugger.

thanks for your suggestion.

I will learn how to use debugger(PVD).
(1)I use “new” -> It is crash, too.
(2)I check pointer and isValid(), they are valid -> It is crash, too.

and I discover that when I don’t enable PxSceneFlag::eENABLE_PCM(PxSceneDesc.flags) in my app -> It does’t crash(When it collide with others)

but I enable PxSceneFlag::eENABLE_PCM in book sample code(PhysX-3.3.0_PC_SDK_Core win32) -> It is OK.

Maybe when using some setting or under some case, make it crash.

thank you very much.


no I didnt mean the PVD Debugger - I mean the Debugger of your IDE.
(The Debugger of Visual Studio 2013 in your case.)

After you figured out how to use the Debugger [of your IDE!!!] you can “see what happens” while you
are stepping throu the lines of code.

I dont know about this PxSceneFlag, mabye you need other flags or some callback / memory managment system to get it to work with PxHeightField, but I dont think so. There should be a reason for it.

[Update thanks Reticula]
I didnt know that there are any PhysX books.


There are some for PhysX 3, like Learning Physics Modeling with PhysX or PhysX Programming

thank you for the links, I didnt know that somebody wrote a book about PhysX :)


Book I use is “Learning Physics Modeling with PhysX”
It provides sample code to learning base PhysX SDK.