I use Bullet Physics and I need to copy an instance of the btTriangleMesh type.
// the variable is a class member
btTriangleMesh triangles;
My aim is to change the collision shape of a body to a new triangle mesh shape. This shape should hold the data currently stored in the triangles variable, even if this variable changes in future. Therefore I need a deep copy.
// in a method change collision shape
btTriangleMesh *copy = new btTriangleMesh(triangles);
btBvhTriangleMeshShape *shape = new btBvhTriangleMeshShape(copy), true, true);
body->setCollisionShape(shape);
// after that, reset variable
triangles = btTriangleMesh();
// change content of variable for next body
// ...
I though instancing a new variable on the heap new btTriangleMesh(triangles) would deep copy the object. But as you can see in the images below, the collision shape of the first body is affected by the next one.
In the image, the white lines represent the collision shape whereas you can see the actual desired shape rendered underneath. This is how the first body looks like, everything is fine here.

And this is how it looks after inserting another body on the right side. The shape of the first body changed to equal the one of the second body. That shouldn't happen. It might be not clear on the image but in 3D you definitely see that the shape of the first body changed to exactly match the one of the second.

How to deep copy a btTriangleMesh? Or am I doing anything else wrong?
By the way the reason for the fact that I use the same variable as source for all triangle shapes is that this variable is filled asynchronously by another thread.