Bullet crash

Post Reply
paokakis
Posts: 29
Joined: Mon Jan 04, 2021 10:31 am

Bullet crash

Post by paokakis »

Hello to the group,

I have some problems working with bullet and I am wondering what I'm doing wrong. Here is some code that works on collision :

Code: Select all

	
	// collision detection
	for (int i = 0; i < world->getDispatcher()->getNumManifolds(); ++i)
	{
		btPersistentManifold* contactManifold = world->getDispatcher()->getManifoldByIndexInternal(i);
		for (int j = 0; j < contactManifold->getNumContacts(); ++j)
		{
			btManifoldPoint& pt = contactManifold->getContactPoint(j);
			if (pt.getDistance() < 0.f)
			{
				const btCollisionObject* obA = contactManifold->getBody0(); // our object
				const btCollisionObject* obB = contactManifold->getBody1(); // enemy object
				void* usrDataA = obB->getUserPointer();
				void* usrDataB = obA->getUserPointer();
			}
		}
	}
Some times I get an exception / crash on the btManifoldPoint and some time on the btCollisionObject a or b. Does someone know why that is? Am I doing something wrong or Bullet has bugs?
Capture.PNG
Capture.PNG (51.07 KiB) Viewed 5895 times
acu192
Posts: 11
Joined: Thu Jan 28, 2021 12:23 am

Re: Bullet crash

Post by acu192 »

C++ is hard. Touch memory wrong *just once* and things explode. In my experience (not just about Bullet, but with any C++ code) the most common issue is that you are sharing a pointer to some objects, the memory for that object gets freed ("delete" in C++) somewhere, but that pointer is still being used by other parts of the code ... and then it gets accessed at some point in the future... and boom, crash! The hard part about this is that (usually) the "bug" is in the code that freed the memory (i.e. it should not have freed it, since the object was still being used elsewhere) ... but that's not where you code crashes. It crashes later on ... at some point in the future ... and the place where is crashes is *not* where the bug is. And, the place where it crashes will *change* from one run to the next (as you mention). Again,the "bug" is not in that place where you code crashes, it is somewhere else (where the memory was freed). This is not the only way to mess up, but it's the most common that I've seen.

It is also hard because you might think, "oh, I just won't free memory so much" ... and great, now your code doesn't crash anymore! But, now you have memory leaks (which of course *will* make your code crash at some point in the distance future when the OS runs out of memory).

Anyway, it's unlikely the bug is in Bullet itself. I (and many many other people) have a deployed app that heavily uses Bullet and have never found such a bug. I'm not saying it impossible, but it's unlikely IMO.

Hope my unhelpful answer... helps some.
acu192
Posts: 11
Joined: Thu Jan 28, 2021 12:23 am

Re: Bullet crash

Post by acu192 »

To expound a bit on my theory...

Take these two lines of code:

Code: Select all

const btCollisionObject* obA = contactManifold->getBody0();
...
void* usrDataB = obA->getUserPointer();

An example of what I'm talking about is: obA is a pointer to memory ... but maybe you freed that memory earlier in the program? If so, you better not touch that memory now or it will go boom on the second line of code above. This situation might have happened if you freed the object but didn't remove it from the dynamics world.
paokakis
Posts: 29
Joined: Mon Jan 04, 2021 10:31 am

Re: Bullet crash

Post by paokakis »

Thank you for your analysis, it was helpful. I moved around some code and modified the tickcallback function and finally I am crash free. The problem was that I was removing objects from the bullet library while I was iterating through the objects at the same time. My fault, and bullet didn't like it. Now I collect the objects that need to be removed and I remove them in a separate step (After iteration)
acu192
Posts: 11
Joined: Thu Jan 28, 2021 12:23 am

Re: Bullet crash

Post by acu192 »

Glad you found your issue!

That's a common pattern, to make a list of thing you *want* to remove, then remove them in a second loop. I have that pattern in several places in my codebase.
Post Reply