Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSVC 2022 crash when dynamic casting #4656

Open
thomaspeer opened this issue Sep 3, 2024 · 0 comments
Open

MSVC 2022 crash when dynamic casting #4656

thomaspeer opened this issue Sep 3, 2024 · 0 comments

Comments

@thomaspeer
Copy link

thomaspeer commented Sep 3, 2024

Hi,

We recently wanted to use dynamic casting to check if some collision shape is a btScaledBvhTriangleMeshShape but we are facing a strange crash in VS2022.

Problem description

Below a sample code we managed to consistently reproduce the issue with:

// Create our basic btTriangleMesh
btTriangleMesh* triangleMesh = new btTriangleMesh();
triangleMesh->addTriangle(btVector3(0, 0, 0), btVector3(0, 1, 0), btVector3(1, 1, 0));

// Create our btBvhTriangleMeshShape
btBvhTriangleMeshShape* triangleMeshShape = new btBvhTriangleMeshShape(triangleMesh, true, true);

// Create our btScaledBvhTriangleMeshShape
btScaledBvhTriangleMeshShape* scaledTriangleMeshShape = new btScaledBvhTriangleMeshShape(triangleMeshShape, btVector3(1, 1, 1));

// Cast the btScaledBvhTriangleMeshShapeto a btCollisionShape (works fine)
btCollisionShape* newCollisionShape = dynamic_cast<btCollisionShape*>(scaledTriangleMeshShape);

// Cast the btCollisionShape to a btBvhTriangleMeshShape  (crashes)
btBvhTriangleMeshShape* newTriangleMeshShape = dynamic_cast<btBvhTriangleMeshShape*>(newCollisionShape);

This also happens if doing as follows:

// Cast the btBvhTriangleMeshShapea btCollisionShape (works fine)
btCollisionShape* newCollisionShape = dynamic_cast<btCollisionShape*>(triangleMeshShape riangleMeshShape);

// Cast the btCollisionShape to a btBvhTriangleMeshShape  (crashes)
btBvhTriangleMeshShape* newTriangleMeshShape = dynamic_cast<btBvhTriangleMeshShape*>(newCollisionShape);

The screenshot below shows the resulting crash in the debugger:

image

Additional findings

I noticed that if I subclass btScaledBvhTriangleMeshShape, the problem vanishes:

class ScaledBvhTriangleMeshShape final : public btScaledBvhTriangleMeshShape
{
public:
    ScaledBvhTriangleMeshShape(btBvhTriangleMeshShape* child_shape, const btVector3& local_scaling)
        : btScaledBvhTriangleMeshShape(child_shape, local_scaling)
    {
    }
};

Maybe this is some alignment issues with MSVC and dynamic cast?

Thanks for helping!

System specs

In case that helps:

  • CPU: Intel 13600KF
  • GPU: AMD 6950 XT
  • RAM: Corsair 64 GB
  • Operating system: Windows 11 Professional 22631.3880
  • IDE: VS2022 Version 17.11.2
  • Build type: Debug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant