bullet3 icon indicating copy to clipboard operation
bullet3 copied to clipboard

segfault when copying btGimpactMeshShape

Open mqnc opened this issue 9 months ago • 0 comments

I've been bitten by the following simplified code:

#include <BulletCollision/CollisionShapes/btTriangleMesh.h>
#include <BulletCollision/Gimpact/btGImpactShape.h>

int main()
{
    btTriangleMesh mesh = btTriangleMesh();

    mesh.addTriangle(
        btVector3(1, 0, 0),
        btVector3(0, 1, 0),
        btVector3(0, 0, 1)
	);
 
    btGImpactMeshShape gimpactShape = btGImpactMeshShape(&mesh);
    gimpactShape.updateBound();

    btGImpactMeshShape gimpactShape2 = btGImpactMeshShape(gimpactShape);
    gimpactShape2.updateBound();

    return 0;
}

This is because the implicit copy constructor just copies m_mesh_parts and the parts then get deleted twice upon destruction. Providing an explicit copy constructor solves this issue:

btGImpactMeshShape(const btGImpactMeshShape& other): btGImpactShapeInterface(other)
{
	m_meshInterface = other.m_meshInterface;
	//m_mesh_parts = other.m_mesh_parts; // this is what the implicit copy ctor does which causes the segfault
	buildMeshParts(m_meshInterface);
}

However, I haven't found many copy ctors throughout the code... Is there some agreement that shapes shouldn't be copied? Could we at least delete the default copy constructor?

mqnc avatar Mar 10 '25 14:03 mqnc