large unexpected impulses on large concave bodies

I'm trying to join/separate lots of bodies at run time. I've tried using the revolute joint and setting its tolerance to zero so that two bodies get welded together, but this becomes very unstable as I add more and more bodies and the whole thing eventually breaks apart. So instead I've made some creative use of the user data pointer and b2XForm to move shapes between bodies and back again so that the bodies connected together are (and therefore act as) a single concave body.

However there is a weird bug(?) where small objects hitting these large compound shapes causes huge impulses that cause them to spin very fast and launch themselves into the ether. I've tried messing around with the update dT and iteration count, but nothing seems to help.

I've uploaded a demo that shows the problem. Red objects stick to each other, green objects don't stick to anything. Press space to break all objects on screen apart, hit return to restart the sim.

Any ideas as to what I could be doing wrong?
Are you destroying the smaller bodies and adding the shapes to the compound body using CreateShape?
Yes. Basically what I've got is a wrapper class around a b2Body, that also keeps a list of b2ShapeDefs. When two bodies collide, I buffer the collision with a b2ContactListener and then move all b2ShapeDefs to the new parent, whilst keeping pointers back to their original owners. I also store a b2XForm in the child object wrapper class that moves it in to the space of the parent wrapper class. All the b2ShapeDefs that are added to the parent have CreateShape called immediately on the parent body followed by SetMassFromShapes. The child body is completely removed from the world.
Figured code speaks louder than words and it may make explaining the odd behaviour easier and that others may benefit from it regardless of how unoptimized/unreadable it may (or may not) be. There are a lot of comments and hopefully it makes sense.

This is what I'm doing in the .exe I uploaded ("private" is currently commented out to be public and for rendering/ease reasons):

physics_object.h
physics_object.cpp

(please let me know if you are going to use this code. I'm kind of curious that way. Also, there a are a few maths functions that I've added to b2Maths.h that may cause this code not to compile on your machine eg "b2XForm b2Mul(T)(const b2XForm& x0, const b2Xform& x1)").
I can't immediately see anything wrong with your code. Try using the latest version of Box2D from SVN. It contains a number of bug fixes that might help.

If that doesn't help, I would need something that I can debug. Preferably, a testbed sample.
Upgraded to 2.0.1. It seems a little bit more stable, but the problems still occur. I've uploaded a new .exe, plus the code I'm using for physics_objects, contact_listeners and the unit test code that sets up the situation in the .exe. I also included b2Maths.h because I've added quite a few things to it that you will need to build my code. Hopefully thats all you need, let me know if there is anything I missed.

Download

Thanks for the help.
s
Is it possible for you to convert this into a testbed example (i.e. extend the Test class in Examples/TestBed/Framework/Test.h)?

The extra math functions are fine.
Done
Just dump all the files (except b2Math.h obviously) in the .zip into 'Examples/Testbed/Tests' and it should work. Let me know how it goes. Thanks again. s.

edit: forgot to mention that launching the bomb crashes the app. the easy way to fix it would be to make the function virtual and chuck an empty function into weld_test. or just don't press space. oh yeah, and hit 'b' to break up objects now.
So one of the guys at my work figured out what the problem was. It's a really easy fix and it doesn't have anything to do with Box2D itself. What was happening was that I wasn't taking into account which object in the collision was the heavier one, so small objects would occasionally have all the shapes from the larger shape move to it. Since the small object had a large rebound impulse off the large object and all the shapes were moved to the small object, it resulted in what looked like the large objects spinning out of control.

Hopefully that made some sort of sense.
I see, you were preserving the velocity of the lighter body. You should add the linear and angular momentums to find the merged velocities:

total_mass * new_velocity = mass1 * velocity1 + mass2 * velocity2

Original thread: https://box2d.org/forum/viewtopic.php?t=1491