Repository: godotengine/godot (language: cpp)

Issue:
JoltPhysics3D: Debug visible collisions crash
### Tested versions

- Reproducible in: v4.4.dev.mono.gh [4cf02312f], v4.4.dev.mono.gh [44dfa7e71], v4.4.dev.custom_build [cee5f8e31]

### System information

Godot v4.4.dev (cee5f8e31) - Fedora Linux 41 (Workstation Edition) on Wayland - X11 display driver, Multi-window, 1 monitor - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3060 Ti (nvidia; 565.77) - Intel(R) Core(TM) i7-10700F CPU @ 2.90GHz (16 threads)

### Issue description

Running a project with debug collisions enabled causes a crash. Number of physics bodies does not seem to matter, it will crash even when there are none. Crash does not occur with GodotPhysics3D.
```
Thread 1 "godot.linuxbsd." received signal SIGILL, Illegal instruction.
0x0000000002ec03ef in CowData<Vector3>::get (this=<optimized out>, p_index=<optimized out>) at ./core/templates/cowdata.h:219
219			CRASH_BAD_INDEX(p_index, size());

 0 in CowData<Vector3>::get of ./core/templates/cowdata.h:219
 1 in Vector<Vector3>::operator[] of ./core/templates/vector.h:101
 2 in Viewport::_notification of scene/main/viewport.cpp:641
 3 in Viewport::_notificationv of ./scene/main/viewport.h:96
 4 in Window::_notificationv of scene/main/window.h:45
 5 in Object::notification of core/object/object.cpp:874
 6 in SceneTree::_process_group of scene/main/scene_tree.cpp:1049
 7 in SceneTree::_process of scene/main/scene_tree.cpp:1136
 8 in SceneTree::physics_process of scene/main/scene_tree.cpp:533
 9 in Main::iteration of main/main.cpp:4449
10 in OS_LinuxBSD::run of platform/linuxbsd/os_linuxbsd.cpp:962
11 in main of platform/linuxbsd/godot_linuxbsd.cpp:85
```

### Steps to reproduce

1. Make a main scene; crash occurs regardless of scene root type
2. Switch to JoltPhysics3D
3. Enable Debug > Visible Collision Shapes
4. Run project

### Minimal reproduction project (MRP)

N/A


Discussion / hints:
> Switch to JoltPhysics3D

Just to be clear, are you using the version of Jolt that's now built in to 4.4, meaning the `Jolt Physics` option in the `physics/3d/physics_engine` project setting, or are you trying to use the Godot Jolt extension in some way? The reason I ask is because the extension uses `JoltPhysics3D` as its registered physics engine name.

I'm not able to reproduce this on Windows 11 nor Linux Mint 22 with the built-in version of Jolt. I've tried a normal build, a normal build with ASan and a .NET build.

~~The fact that it's crashing with `SIGILL, Illegal instruction` strikes me as odd though. If you're not using the Godot Jolt extension, what arguments are you passing when compiling Godot?~~

EDIT: Nevermind the above, I just realized that's how `CRASH_BAD_INDEX`/`__builtin_trap` works.
I was able to reproduce it on Linux, with the built-in version of Jolt. I am however still not able to reproduce the crash on Windows for some reason.

I'll take a closer look.

Fix patch:
diff --git a/modules/jolt_physics/spaces/jolt_contact_listener_3d.h b/modules/jolt_physics/spaces/jolt_contact_listener_3d.h
index 41b3c9ef3bb2..e18a8a9bf1a8 100644
--- a/modules/jolt_physics/spaces/jolt_contact_listener_3d.h
+++ b/modules/jolt_physics/spaces/jolt_contact_listener_3d.h
@@ -93,7 +93,7 @@ class JoltContactListener3D final
 
 #ifdef DEBUG_ENABLED
 	PackedVector3Array debug_contacts;
-	std::atomic_int debug_contact_count;
+	std::atomic_int debug_contact_count = 0;
 #endif
 
 	virtual void OnContactAdded(const JPH::Body &p_body1, const JPH::Body &p_body2, const JPH::ContactManifold &p_manifold, JPH::ContactSettings &p_settings) override;


Test patch: