Repository: godotengine/godot (language: cpp)

Issue:
Game window, BadWindow error when using 3d mode and clicking on 2d scene
### Tested versions

Godot v4.4.beta2

### System information

Fedora Linux 41 (KDE Plasma) on Wayland - X11 display driver, Multi-window

### Issue description

Using 3d mode in game view and interacting with 2d stuff crashes game window, sometimes without error code.
If you have used 2d mode first and then switch to 3d mode and click on something or if you press space while in 3d mode when the game as focus it seems to always prints this error:

```
  ERROR: platform/linuxbsd/x11/display_server_x11.cpp:1218 - Unhandled XServer error: BadWindow (invalid Window parameter)
  ERROR:    Major opcode of failed request: 20
  ERROR:    Serial number of failed request: 0
  ERROR:    Current serial number in output stream: 118894
````
_Serial number is different_


https://github.com/user-attachments/assets/4103f07e-41ab-4381-aa08-f3a66ddba018


Found this when I had a 3d scene with only a Camera3D in SubViewportContainer

### Steps to reproduce

Have a 2d scene
Open game window
Switch to 3d mode
Click on something

### Minimal reproduction project (MRP)

N/A


Discussion / hints:
Thanks again for testing and reporting the issue. Very appreciated!

I'm able to reproduce on 4.4 beta 2. With the version on master, the error is not displayed anymore but the game just hang.

**Edited: The same hang occurs when the game is not embedded at all.**
I was able to debug and recreate the bug. It's because there's no 3D Camera in the scene. It's not linked to the Game View or Linux. The Bad Window error you got was because the Game Window did not exist anymore following the crash.

![Image](https://github.com/user-attachments/assets/9c2a1a24-0799-46f2-b44f-b1b369c89739)

cc @YeldhamDev 

Fix patch:
diff --git a/scene/debugger/scene_debugger.cpp b/scene/debugger/scene_debugger.cpp
index 5d996bfdfa9b..64087cdaf296 100644
--- a/scene/debugger/scene_debugger.cpp
+++ b/scene/debugger/scene_debugger.cpp
@@ -1868,7 +1868,6 @@ void RuntimeNodeSelect::_update_view_2d() {
 #ifndef _3D_DISABLED
 void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<SelectResult> &r_items) {
 	Window *root = SceneTree::get_singleton()->get_root();
-	Camera3D *camera = root->get_viewport()->get_camera_3d();
 	Vector3 ray, pos, to;
 	if (root->get_viewport()->is_camera_3d_override_enabled()) {
 		Viewport *vp = root->get_viewport();
@@ -1876,6 +1875,11 @@ void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<Select
 		pos = vp->camera_3d_override_project_ray_origin(p_pos);
 		to = pos + ray * vp->get_camera_3d_override_properties()["z_far"];
 	} else {
+		Camera3D *camera = root->get_viewport()->get_camera_3d();
+		if (!camera) {
+			return;
+		}
+
 		ray = camera->project_ray_normal(p_pos);
 		pos = camera->project_ray_origin(p_pos);
 		to = pos + ray * camera->get_far();


Test patch: