Game Development Community

help with pointer chash

by deepscratch · in Torque 3D Professional · 07/08/2009 (1:13 pm) · 10 replies

hi all,
so I'm working on vehicle tire decals,
I've managed to get decals on the terrain from tires, but after traveling for a short distance, the engine crashes, pointing at the first image,

img18.imageshack.us/img18/5843/error1qzm.jpg

and shows this


img18.imageshack.us/img18/409/error2l.jpg

and the only thing I can think of that is doing it is this:

Wheel* wend = &mWheel[mDataBlock->wheelCount];
      for (Wheel* wheel = mWheel; wheel < wend; wheel++) 
		{
            pos = ((0 == wheel) ? posRight : posLeft);
            offset = ((0 == wheel) ? offset : -offset);
            //offset = mDataBlock->decalOffset;

         Point3F rot, pos;
         RayInfo rInfo;
         MatrixF mat = getRenderTransform();
         mat.getColumn(1, &rot);
         mat.mulP(Point3F(offset,0.0f,0.0f), &pos);
         if (gClientContainer.castRay(Point3F(pos.x, pos.y, pos.z + 0.01f),
            Point3F(pos.x, pos.y, pos.z - 2.0f ),
            STATIC_COLLISION_MASK | VehicleObjectType, &rInfo))
			{
            Material* material = ( rInfo.material ? dynamic_cast< Material* >( rInfo.material->getMaterial() ) : 0 );
            // Put track marks on surface, if appropriate for material.
            if( mDataBlock->decalData != 0 )
            {
               Point3F normal;
               Point3F tangent;
               mObjToWorld.getColumn( 0, &tangent );
               mObjToWorld.getColumn( 2, &normal );
               gDecalManager->addDecal( rInfo.point, normal, tangent, mDataBlock->decalData );                  
            }

and I think, in particular, this part:

Wheel* wend = &mWheel[mDataBlock->wheelCount];
      for (Wheel* wheel = mWheel; wheel < wend; wheel++) 
		{
            pos = ((0 == wheel) ? posRight : posLeft);
            offset = ((0 == wheel) ? offset : -offset);

now if I drive a bit and stop till the decals fade, then drive futher, it works, till a certain amount of decals ar layed down, then crashes.

please could someone help me with this crash?
thanks.

#1
07/08/2009 (1:47 pm)
What's the purpose of this code?
pos = ((0 == wheel) ? posRight : posLeft);   
            offset = ((0 == wheel) ? offset : -offset);

this looks like it's checking to see if your wheel is a null pointer. Is that what you want?


edit: Deleted part about accessing array as a pointer. You're not supposed to do it anymore, but the wheel code is littered with it.
#2
07/08/2009 (1:52 pm)
your 'wheel' is a memory adress,it is not an integer
is that you want ?

pos = ((0 == wheel) ? posRight : posLeft);  
offset = ((0 == wheel) ? offset : -offset);

what is supposed to do here ?
#3
07/08/2009 (2:00 pm)
If you're trying to add the track on the right for the first wheel, and the left for the second wheel, do it like this. And while you're at it, rewrite the loop with an index:

Plus - the "offset" is being reset every wheel, so the "offset = offset" part will not give you a positive offset after it's been set to -offset. You need a second variable to keep offset pure.

for (int i=0;i<mDataBlock->wheelCount];i++)
      {   
            Wheel* wheel = &mWheel[i];   
            if (i&1)
            {
              pos = posRight;
            }
            else
            {
              pos = posLeft;
            }

edit: sorry, I was in a hurry posting this before.

Plus - And this is the most important bit - you need to post a CALLSTACK when you get an error like this. What this tells us that you have an invalid pointer, but we don't know which one. Most likely you are accessing something after it has been deallocated, or beyond the bounds of the array.
#4
07/08/2009 (11:32 pm)
thanks guys, for looking into this,
heres the call stack

img268.imageshack.us/img268/2086/callstack.jpg

so it is a pointer problem
#5
07/09/2009 (5:40 am)
whats strange, is that the code comes from class player, with a small change.
what would cause class vehicle to crash, and class player not?

the difference being:

bool triggeredLeft = false;
      bool triggeredRight = false;
      F32 offset = 0.0f;
      if(mShapeInstance->getTriggerState(1)) {
         triggeredLeft = true;
         offset = -mDataBlock->decalOffset;
      }
      else if(mShapeInstance->getTriggerState(2)) {
         triggeredRight = true;
         offset = mDataBlock->decalOffset;
      }


      if (triggeredLeft || triggeredRight)
      {
         Point3F rot, pos;
         RayInfo rInfo;
         MatrixF mat = getRenderTransform();
         mat.getColumn(1, &rot);
         mat.mulP(Point3F(offset,0.0f,0.0f), &pos);
         if (gClientContainer.castRay(Point3F(pos.x, pos.y, pos.z + 0.01f),
            Point3F(pos.x, pos.y, pos.z - 2.0f ),
            STATIC_COLLISION_MASK | VehicleObjectType, &rInfo))
         {
            Material* material = ( rInfo.material ? dynamic_cast< Material* >( rInfo.material->getMaterial() ) : 0 );

            // Put footprints on surface, if appropriate for material.

            if( /*material && material->mShowFootprints
                &&*/ mDataBlock->decalData != 0 )
            {
               Point3F normal;
               Point3F tangent;
               mObjToWorld.getColumn( 0, &tangent );
               mObjToWorld.getColumn( 2, &normal );
               gDecalManager->addDecal( rInfo.point, normal, tangent, mDataBlock->decalData );                  
            }

from player

and

Wheel* wend = &mWheel[mDataBlock->wheelCount];
      for (Wheel* wheel = mWheel; wheel < wend; wheel++) 
		{
            pos = ((0 == wheel) ? posRight : posLeft);
            offset = ((0 == wheel) ? offset : -offset);

         MatrixF mat = getRenderTransform();
         mat.getColumn(1, &rot);
         mat.mulP(Point3F(offset,0.0f,0.0f), &pos);
         if (gClientContainer.castRay(Point3F(pos.x, pos.y, pos.z + 0.01f),
            Point3F(pos.x, pos.y, pos.z - 2.0f ),
            STATIC_COLLISION_MASK | VehicleObjectType, &rInfo))

            // Put track marks on surface
            if( mDataBlock->decalData != 0 )
            {
               Point3F normal;
               Point3F tangent;
               mObjToWorld.getColumn( 0, &tangent );
               mObjToWorld.getColumn( 2, &normal );
               gDecalManager->addDecal( rInfo.point, normal, tangent, mDataBlock->decalData );                  
            }

in vehicle
#6
07/09/2009 (6:11 am)
Your crash is an uninitialized vertex buffer. Most likely you are putting in so many decals that you are overflowing some buffer that is not being bounds-checked. This would explain why it works after you rest a while (because decals get freed up).

I think someone who is more knowledgeable about how the vertex buffers are allocated will need to answer this.
#7
07/09/2009 (6:48 am)
I believe you are right, Jaimi
I discovered that just standing and looking at the vehicle for long enough causes a crash.
the wheels lay decals each process tick, and 4 for each vehicle in the scene can become a lot, really quickly
#8
07/09/2009 (7:08 am)
ok, I added an "if" to the code,

if (speed > 1.0f)

so now it will lay decals only if I am moving in the vehicle, I also dropped the lifespan of the decals, time for a test drive, see if I can crash the engine.

wow, so many puns, yet none intended ;o
#9
07/09/2009 (12:35 pm)
I think its cool that you were able to get something working with the existing decal system with very little work, but this is not the way I would do it...

Basically its going to perform and look better if you use decal "strips" that get dynamically lengthend rather than stacking lots of tire-segment shaped decals on top of each other.

But of course this system does not exist... the closest thing is the decalRoad. But unlike the decalRoad it needs code for limiting its max length, fading out the old end as you add onto the front, and reducing the number of reclips required as much as possible (since it will be changing constantly). The decalRoad is not very efficient at all when it reclips (terrain verts) since it reclips the entire road even when perhaps only one node changed. For a lengthy tiretrack and a high resolution terrain this would be unacceptable.
#10
07/09/2009 (12:50 pm)
yeah, the fact that it lays down so many decals on top of each other makes the engine reach its max on decals very quickly, although, if you wait for the first decals to fade before you go off for a drive, then you can drive about for ever with out a crash

also, an upside to the layering of the decals, is that they tend to take corners quite well.
the decal that I am using is almost circular, which helps on the curves.

any idea on how to optimize the resource I posted? its still bugging me a bit, and will still occasionaly crash out.

as I've said many times before, I'm not a coder, at all, I want certain things in the engine, and I just work out a way to do it, not always the best way, but most of the time a working way