same old removing objects causes a crash problem...pls help

Official forum for the Chipmunk2D Physics Library.
Post Reply
knapshots
Posts: 11
Joined: Sat Mar 05, 2011 11:25 pm
Contact:

same old removing objects causes a crash problem...pls help

Post by knapshots »

so,,
here are the steps to add and remove......and the remove part is causing an assert,,

Code: Select all

if(shapeType==STATIC)
{
    isStatic=true;
    cpBodyDestroy(theBody);  //memory if allocated for normal body get rid of it
    theBody=cpBodyNewStatic();
    cpBodySetPos(theBody,theRealPos); 

}
else
{
    isStatic=false;
        cpBodySetMass(theBody,this->theMass);
        cpBodySetPos(theBody,theRealPos);
}

//shape definition...assuming for now its a box
theShape=cpBoxShapeNew(theBody,5,5);


             cpSpaceAddBody(theWorld,theBody);
             cpSpaceAddShape(theWorld,theShape);

//docs mention that u neednt  use addstaticshape if u aint using a rogue body.....so i am not using it



///heres the remove part...not part of a collision process...the updatestep is disabled and nothing else is
//messing with my physics objects
cpSpaceRemoveBody(theWorld,this->theBody);
  theShape->data=NULL;
  cpSpaceRemoveShape(theWorld,this->theShape);
   cpBodyFree(this->theBody);
    cpShapeFree(this->theShape);

 

for the record, i tried removing the cpSpaceRemoveBody step if the shape is static......
it does crash if i dont remove that step for static bodies, but even the removeshape step crashes
for static bodies regardless of removebody is called or not....where the heck am i goin wrong.....

thanks for reading.... :)
pls help me if u can
knapshots
Posts: 11
Joined: Sat Mar 05, 2011 11:25 pm
Contact:

Re: same old removing objects causes a crash problem...pls help

Post by knapshots »

btw.....i think this is a problem exclusive to shapes which have the sensor attribute set to true... i am not sure though
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: same old removing objects causes a crash problem...pls help

Post by slembcke »

Not sure exactly what is causing your issue, but I think I see some other bugs in there.

If your if(shapeType==STATIC) block you are trying to be clever and not reallocate your body variable, but by mixing new and destroy and causing a memory leak. Unless you are writing a language binding and need the extra memory flexibility doing something fancy with memory allocation like allocating things on the stack, never ever use destroy. Stick to new/free. Trying to be clever with memory management usually just leads to hard to find dangling pointer bugs or leaks.

You are also calling cpSpaceAddBody() for static bodies. This is fine if you don't have gravity, but is sort of a waste of CPU cycles and disastrous if you do have gravity.

I don't really see anything else that would be a problem. What is the stack trace from the debugger?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
knapshots
Posts: 11
Joined: Sat Mar 05, 2011 11:25 pm
Contact:

Re: same old removing objects causes a crash problem...pls help

Post by knapshots »

As always, turns out it was my mistake..... and ya u were rite..(obviously if u are the creator of the api u wont be wrong :) )
i shudnt add a body if its static.... that wasnt the mistake though.....


for ppl who might be stuck ,
the thing was i generated signals on post collision solve completion using the post step callback for the objects to do their collision thing....but the thing was there wasnt enuf time between the post solve completion and the next timer tick to delete the object resulting in the same old cpSpace locked assert scenario...

so what worked for me was to maintain a list of pointers of objects to be deleted..... and just append the pointer of the object that had to be deleted to the list and in the next timer tick perform primitive garbage collection as i like to call it for each and every pointer in the list.....that worked....thanks again slembcke


btw....this is the code snippet for deletion....if it can help someone and if aint wrong,then no harm in sharing it as well...:)

Code: Select all

   if(theObject->shapeType==STATIC)
          {

                


                     cpSpaceRemoveStaticShape(theWorld,theObject->theShape);
                       cpBodyFree(theObject->theBody);
                       cpShapeFree(theObject->theShape);

                 




           }

            else if(theObject->shapeType==ACTIVE)
           {

                cpSpaceRemoveBody(theWorld,theObject->theBody);
                cpSpaceRemoveShape(theWorld,theObject->theShape);
                  cpBodyFree(theObject->theBody);
                  cpShapeFree(theObject->theShape);
            }



And this is the code for garbage collection and deletion

Code: Select all


//qphysicsobject is my glue class...this is just to help those stuck with similar logical errors...
 void removeWorldObject(qPhysicsObject* theObject)
      {
 ///schedule deletion by calling this function...dont delete directly without knowing if the spacestep is underway or not
          theGarbageCollector->append(theObject); //theGarbageCollector is simply a list container of pointer objects
          return;
         }


    void onTick()
    {

          float timeStep=1.0/(float)fpsDude;

       cpSpaceStep(theWorld,timeStep);
      // cpSpaceRehashStatic(theWorld); not needed most of the time..
          cpSpaceHashEach(theWorld->activeShapes, &updateShape, NULL);
 
 qPhysicsObject* theObject;
          foreach(theObject,*theGarbageCollector)
          {
              _removeWorldObject(theObject); //ur delete code for the glue object and the earlier shown deletion code for the
                                                                   //chipmunk object goes over here
              theGarbageCollector->removeOne(theObject);//remove it from the list
          }


    }


well....thats all folks....thanks again for ur patient reading :)
Post Reply

Who is online

Users browsing this forum: Heise IT-Markt [Crawler] and 8 guests