Memory Leak with collision detection?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Memory Leak with collision detection?

Postby adamsna » Thu Nov 13, 2008 7:06 pm

I am (horribly failing at) integrating Newton Physics into a game engine I am writing and I have found a memory leak.
I will say that I am not very familiar with Newton (yet) but this is what I came up with:

Code: Select all
int CheckForCollision(Irr2DObj& obj_A, Irr2DObj& obj_B)
{
const int nContacts = 4;
float contacts[3 * nContacts];
float normals[3 * nContacts];
float penetration[3 * nContacts ];

dMatrix t1, t2;
NewtonCollision* obj1_col = NewtonCreateBox(IrrPhysics::nworld, obj_A->getX(), obj_A->getY(), 0, NULL);
NewtonCollision* obj2_col = NewtonCreateBox(IrrPhysics::nworld, obj_B->getX(), obj_B->getY(), 0, NULL);
NewtonBody* obj1 = NewtonCreateBody(IrrPhysics::nworld, obj1_col);
NewtonBody* obj2 = NewtonCreateBody(IrrPhysics::nworld, obj2_col);

NewtonBodyGetMatrix( obj1, &t1[0][0] );
NewtonBodyGetMatrix( obj2, &t2[0][0] );
//Check for collision between collision meshes,
// returns number of contact points
int nHits = NewtonCollisionCollide( IrrPhysics::nworld,nContacts,
                    NewtonBodyGetCollision(obj1), &t1[0][0],
                    NewtonBodyGetCollision(obj2), &t2[0][0],
                    contacts,
                    normals,
                    penetration);
//Collision detected if nHits > 0
/*if( nHits > 0)
return true;
return false;*/
delete obj1_col;
delete obj2_col;
delete obj1;
delete obj2;
return nHits;

}


I call this function in a loop (bad idea probably) to detect collision between 2 2D objects. The 2 objects I pass in are aware of its position in the X, Y direction (through getX() and getY() functions). For now I am going to assume that all sprites (2D objects) are squares. My problem is both that when the function is called it seems to eat memory and it doesn't work. I assume that I am completely doing it wrong. The purpose of this function is to detect collision between 2 objects. Can anyone lend a hand?

Let me know if you need more info/code.
adamsna
 
Posts: 3
Joined: Sun Aug 24, 2008 4:50 pm

Re: Memory Leak with collision detection?

Postby martinsm » Fri Nov 14, 2008 4:40 am

You never ever call delete on pointers that are returned by Newton functions. Instead you use appropriate functions to delete them:
to delete NewtonBody - NewtonDestroyBody
to delete NewtonCollision - NewtonReleaseCollision
to delete NewtonWorld - NewtonDestroy
to delete NewtonJoint - NewtonDestroyJoint
martinsm
 
Posts: 86
Joined: Mon Dec 19, 2005 3:15 pm
Location: Latvia

Re: Memory Leak with collision detection?

Postby JernejL » Fri Nov 14, 2008 5:32 am

martinsm solved the memory leak issue for you, but as for your use of newton, it will be probably a lot faster if you reuse the collision box objects rather than create & destroy them during each check.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Memory Leak with collision detection?

Postby adamsna » Fri Nov 14, 2008 1:27 pm

martinsm wrote:You never ever call delete on pointers that are returned by Newton functions. Instead you use appropriate functions to delete them:
to delete NewtonBody - NewtonDestroyBody
to delete NewtonCollision - NewtonReleaseCollision
to delete NewtonWorld - NewtonDestroy
to delete NewtonJoint - NewtonDestroyJoint

Thank you very much, that works...I must've skipped over that part in the API ;):
Remarks
Collision primitives are reference counted objects. The application should call NewtonReleaseCollision in order to release references to the object. Neglecting to release references to collision primitives is a common cause of memory leaks. Collision primitives can be reused with more than one body. This will reduce the amount of memory used be the engine, as well as speed up some calculations.


Delfi wrote:martinsm solved the memory leak issue for you, but as for your use of newton, it will be probably a lot faster if you reuse the collision box objects rather than create & destroy them during each check.

I will keep that in mind for the future (for not only with using it with 2D objects that I am currently working with, but also 3D objects that are working but I figured I could optimize my code, so that is a useful tip) but right now I just want to get it to work. Right now we fixed the memory leak issue (yay!), but it's not detecting collision. I suspect from reading the wiki and the API that the X,Y,Z parameters in NewtonCreateBox are the dimensions of the box and not the coords of the object. My question is now appears to be how to tell Newton to move the object to a specific position in the "NewtonWorld" (and thus ultimately detect collision)?
I'm not looking for a crisp clean way to do it, just as long as I can detect collision is my first goal, second goal will be to optimize.

Btw, this is my current code:
Code: Select all
int CheckForCollision(Irr2DObj& obj_A, Irr2DObj& obj_B)
{
const int nContacts = 4;
float contacts[3 * nContacts];
float normals[3 * nContacts];
float penetration[3 * nContacts ];

dMatrix t1, t2;
NewtonCollision* obj1_col = NewtonCreateBox(IrrPhysics::nworld, obj_A->getX(), obj_A->getY(), 0, NULL);
NewtonCollision* obj2_col = NewtonCreateBox(IrrPhysics::nworld, obj_B->getX(), obj_B->getY(), 0, NULL);
NewtonBody* obj1 = NewtonCreateBody(IrrPhysics::nworld, obj1_col);
NewtonBody* obj2 = NewtonCreateBody(IrrPhysics::nworld, obj2_col);

NewtonBodyGetMatrix( obj1, &t1[0][0] );
NewtonBodyGetMatrix( obj2, &t2[0][0] );

//Check for collision between collision meshes,
// returns number of contact points
int nHits = NewtonCollisionCollide( IrrPhysics::nworld,nContacts,
                    NewtonBodyGetCollision(obj1), &t1[0][0],
                    NewtonBodyGetCollision(obj2), &t2[0][0],
                    contacts,
                    normals,
                    penetration);
//Collision detected if nHits > 0
/*if( nHits > 0)
return true;
return false;*/
NewtonReleaseCollision(IrrPhysics::nworld, obj1_col);
NewtonReleaseCollision(IrrPhysics::nworld, obj2_col);
NewtonDestroyBody(IrrPhysics::nworld, obj1);
NewtonDestroyBody(IrrPhysics::nworld, obj2);
return nHits;

}
adamsna
 
Posts: 3
Joined: Sun Aug 24, 2008 4:50 pm

Re: Memory Leak with collision detection?

Postby martinsm » Fri Nov 14, 2008 4:29 pm

Why are you creating bodies at all if you want just to calculate collisions between two NewtonCollision's?
NewtonCollisionCollide expects two collision objects and their transformation matrices (4x4) in world. So just put your positions of these collisions in matrixA and matrixB arguments and you're done. No need to create bodies.

If you still want/need newton bodies to be created then use NewtonBodySetMatrix function to specify body position/rotation in world.
martinsm
 
Posts: 86
Joined: Mon Dec 19, 2005 3:15 pm
Location: Latvia

Re: Memory Leak with collision detection?

Postby adamsna » Fri Nov 14, 2008 5:45 pm

martinsm wrote:Why are you creating bodies at all if you want just to calculate collisions between two NewtonCollision's?
NewtonCollisionCollide expects two collision objects and their transformation matrices (4x4) in world. So just put your positions of these collisions in matrixA and matrixB arguments and you're done. No need to create bodies.

If you still want/need newton bodies to be created then use NewtonBodySetMatrix function to specify body position/rotation in world.

Ahh thank you for your advice. I finally got it to work to detect collision between 2 objects in the 2D. I had to add more methods such as the object to know its own height and width and this is what I came up with (excuse the commented code of course)
Code: Select all
int CheckForCollision(Irr2DObj& obj_A, Irr2DObj& obj_B)
{
const int nContacts = 4;
float contacts[3 * nContacts];
float normals[3 * nContacts];
float penetration[3 * nContacts ];

//dMatrix t1, t2;
matrix4 mat_A,mat_B;
mat_A.setTranslation(obj_A->getPosition());
mat_B.setTranslation(obj_B->getPosition());
NewtonCollision* obj1_col = NewtonCreateBox(IrrPhysics::nworld, obj_A->getLength(), obj_A->getHeight(), 1, NULL);
NewtonCollision* obj2_col = NewtonCreateBox(IrrPhysics::nworld, obj_B->getLength(), obj_B->getHeight(), 1, NULL);
//NewtonCollision* obj2_col = NewtonCreateBox(IrrPhysics::nworld, 2, 10, 1, NULL);
//NewtonBody* obj1 = NewtonCreateBody(IrrPhysics::nworld, obj1_col);
//NewtonBody* obj2 = NewtonCreateBody(IrrPhysics::nworld, obj2_col);

//NewtonBodyGetMatrix( obj1, &t1[0][0] );
//NewtonBodyGetMatrix( obj2, &t2[0][0] );

//Check for collision between collision meshes,
// returns number of contact points
int nHits = NewtonCollisionCollide( IrrPhysics::nworld,nContacts,
                    obj1_col, (float*)&mat_A,
                    obj2_col, (float*)&mat_B,
                    contacts,
                    normals,
                    penetration);
//Collision detected if nHits > 0
/*if( nHits > 0)
return true;
return false;*/
NewtonReleaseCollision(IrrPhysics::nworld, obj1_col);
NewtonReleaseCollision(IrrPhysics::nworld, obj2_col);
//NewtonDestroyBody(IrrPhysics::nworld, obj1);
//NewtonDestroyBody(IrrPhysics::nworld, obj2);
return nHits;

}


This is more of a side question, but if an object has a "z" of 0, doesn't that make it a 2D object because it doesn't have any depth? In my code I had to use 1...which doesn't matter in a 2D environment because it could be 1 or 100 and it should still have the same effect, right?
adamsna
 
Posts: 3
Joined: Sun Aug 24, 2008 4:50 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 12 guests

cron