Repository: godot
Issue: Baking a lightmap on a scene that already has a lightmap causes crash
Labels: bug,topic:rendering,crash,topic:3d

### Tested versions

- first found in godot 4.3.1 [90f21a35c]

### System information

Fedora 41 - Godot 4.3[90d21a35c] 

### Issue description

when baking a lightmap when another lightmap is already assigned to a LightmapGI node causes a crash

```
ERROR: Trying to unreference a SafeRefCount which is already zero is wrong and a symptom of it being misused.
Upon a SafeRefCount reaching zero any object whose lifetime is tied to it, as well as the ref count itself, must be destroyed.
Moreover, to guarantee that, no multiple threads should be racing to do the final unreferencing to zero.
   at: _check_unref_safety (./core/templates/safe_refcount.h:187)

================================================================
handle_crash: Program crashed with signal 4
Engine version: Godot Engine v4.3.1.rc.custom_build (90f21a35c71107d2afe95a3875bb761ba2170d90)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] /lib64/libc.so.6(+0x1a090) [0x7fd01fd9a090] (??:0)
[2] RefCounted::unreference() (/home/jwoon/Documents/GodotTemplates/godot/./core/templates/safe_refcount.h:187 (discriminator 2))
[3] Ref<LightmapGIData>::unref() (/home/jwoon/Documents/GodotTemplates/godot/./core/object/ref_counted.h:209 (discriminator 1))
[4] Ref<LightmapGIData>::ref(Ref<LightmapGIData> const&) (/home/jwoon/Documents/GodotTemplates/godot/./core/object/ref_counted.h:65)
[5] LightmapGI::set_light_data(Ref<LightmapGIData> const&) (/home/jwoon/Documents/GodotTemplates/godot/./core/object/ref_counted.h:117)
[6] LightmapGI::bake(Node*, String, bool (*)(float, String const&, void*, bool), void*) (/home/jwoon/Documents/GodotTemplates/godot/./scene/3d/lightmap_gi.cpp:1346)
[7] LightmapGIEditorPlugin::_bake_select_file(String const&) (/home/jwoon/Documents/GodotTemplates/godot/./editor/plugins/lightmap_gi_editor_plugin.cpp:71 (discriminator 4))
[8] void call_with_variant_args_helper<LightmapGIEditorPlugin, String const&, 0ul>(LightmapGIEditorPlugin*, void (LightmapGIEditorPlugin::*)(String const&), Variant const**, Callable::CallError&, IndexSequence<0ul>) (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/binder_common.h:304 (discriminator 6))
[9] void call_with_variant_args<LightmapGIEditorPlugin, String const&>(LightmapGIEditorPlugin*, void (LightmapGIEditorPlugin::*)(String const&), Variant const**, int, Callable::CallError&) (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/binder_common.h:418)
[10] CallableCustomMethodPointer<LightmapGIEditorPlugin, String const&>::call(Variant const**, int, Variant&, Callable::CallError&) const (/home/jwoon/Documents/GodotTemplates/godot/./core/object/callable_method_pointer.h:103)
[11] Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/callable.cpp:57)
[12] Object::emit_signalp(StringName const&, Variant const**, int) (/home/jwoon/Documents/GodotTemplates/godot/./core/object/object.cpp:1190)
[13] Node::emit_signalp(StringName const&, Variant const**, int) (/home/jwoon/Documents/GodotTemplates/godot/./scene/main/node.cpp:3899)
[14] Error Object::emit_signal<String>(StringName const&, String) (/home/jwoon/Documents/GodotTemplates/godot/./core/object/object.h:936)
[15] EditorFileDialog::_save_confirm_pressed() (/home/jwoon/Documents/GodotTemplates/godot/./editor/gui/editor_file_dialog.cpp:402 (discriminator 4))
[16] void call_with_variant_args_helper<EditorFileDialog>(EditorFileDialog*, void (EditorFileDialog::*)(), Variant const**, Callable::CallError&, IndexSequence<>) (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/binder_common.h:304 (discriminator 4))
[17] void call_with_variant_args<EditorFileDialog>(EditorFileDialog*, void (EditorFileDialog::*)(), Variant const**, int, Callable::CallError&) (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/binder_common.h:418)
[18] CallableCustomMethodPointer<EditorFileDialog>::call(Variant const**, int, Variant&, Callable::CallError&) const (/home/jwoon/Documents/GodotTemplates/godot/./core/object/callable_method_pointer.h:103)
[19] Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/callable.cpp:57)
[20] Object::emit_signalp(StringName const&, Variant const**, int) (/home/jwoon/Documents/GodotTemplates/godot/./core/object/object.cpp:1190)
[21] Node::emit_signalp(StringName const&, Variant const**, int) (/home/jwoon/Documents/GodotTemplates/godot/./scene/main/node.cpp:3899)
[22] Error Object::emit_signal<>(StringName const&) (/home/jwoon/Documents/GodotTemplates/godot/./core/object/object.h:936)
[23] AcceptDialog::_ok_pressed() (/home/jwoon/Documents/GodotTemplates/godot/./scene/gui/dialogs.cpp:137 (discriminator 1))
[24] void call_with_variant_args_helper<AcceptDialog>(AcceptDialog*, void (AcceptDialog::*)(), Variant const**, Callable::CallError&, IndexSequence<>) (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/binder_common.h:304 (discriminator 4))
[25] void call_with_variant_args<AcceptDialog>(AcceptDialog*, void (AcceptDialog::*)(), Variant const**, int, Callable::CallError&) (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/binder_common.h:418)
[26] CallableCustomMethodPointer<AcceptDialog>::call(Variant const**, int, Variant&, Callable::CallError&) const (/home/jwoon/Documents/GodotTemplates/godot/./core/object/callable_method_pointer.h:103)
[27] Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/callable.cpp:57)
[28] Object::emit_signalp(StringName const&, Variant const**, int) (/home/jwoon/Documents/GodotTemplates/godot/./core/object/object.cpp:1190)
[29] Node::emit_signalp(StringName const&, Variant const**, int) (/home/jwoon/Documents/GodotTemplates/godot/./scene/main/node.cpp:3899)
[30] Error Object::emit_signal<>(StringName const&) (/home/jwoon/Documents/GodotTemplates/godot/./core/object/object.h:936)
[31] BaseButton::_pressed() (/home/jwoon/Documents/GodotTemplates/godot/./scene/gui/base_button.cpp:137 (discriminator 1))
[32] BaseButton::on_action_event(Ref<InputEvent>) (/home/jwoon/Documents/GodotTemplates/godot/./scene/gui/base_button.cpp:171)
[33] BaseButton::gui_input(Ref<InputEvent> const&) (/home/jwoon/Documents/GodotTemplates/godot/./scene/gui/base_button.cpp:68 (discriminator 2))
[34] Control::_call_gui_input(Ref<InputEvent> const&) (/home/jwoon/Documents/GodotTemplates/godot/./scene/gui/control.cpp:1830)
[35] Viewport::_gui_call_input(Control*, Ref<InputEvent> const&) (/home/jwoon/Documents/GodotTemplates/godot/./scene/main/viewport.cpp:1568)
[36] Viewport::_gui_input_event(Ref<InputEvent>) (/home/jwoon/Documents/GodotTemplates/godot/./scene/main/viewport.cpp:1837 (discriminator 8))
[37] Viewport::push_input(Ref<InputEvent> const&, bool) (/home/jwoon/Documents/GodotTemplates/godot/./scene/main/viewport.cpp:3259 (discriminator 2))
[38] Window::_window_input(Ref<InputEvent> const&) (/home/jwoon/Documents/GodotTemplates/godot/./scene/main/window.cpp:1680)
[39] Viewport::_sub_windows_forward_input(Ref<InputEvent> const&) (/home/jwoon/Documents/GodotTemplates/godot/./scene/main/viewport.cpp:2986)
[40] Viewport::push_input(Ref<InputEvent> const&, bool) (/home/jwoon/Documents/GodotTemplates/godot/./scene/main/viewport.cpp:3243 (discriminator 2))
[41] Window::_window_input(Ref<InputEvent> const&) (/home/jwoon/Documents/GodotTemplates/godot/./scene/main/window.cpp:1680)
[42] void call_with_variant_args_helper<Window, Ref<InputEvent> const&, 0ul>(Window*, void (Window::*)(Ref<InputEvent> const&), Variant const**, Callable::CallError&, IndexSequence<0ul>) (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/binder_common.h:304 (discriminator 6))
[43] void call_with_variant_args<Window, Ref<InputEvent> const&>(Window*, void (Window::*)(Ref<InputEvent> const&), Variant const**, int, Callable::CallError&) (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/binder_common.h:418)
[44] CallableCustomMethodPointer<Window, Ref<InputEvent> const&>::call(Variant const**, int, Variant&, Callable::CallError&) const (/home/jwoon/Documents/GodotTemplates/godot/./core/object/callable_method_pointer.h:103)
[45] Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/callable.cpp:57)
[46] Variant Callable::call<Ref<InputEvent> >(Ref<InputEvent>) const (/home/jwoon/Documents/GodotTemplates/godot/./core/variant/variant.h:875)
[47] DisplayServerWayland::_dispatch_input_event(Ref<InputEvent> const&) (/home/jwoon/Documents/GodotTemplates/godot/platform/linuxbsd/wayland/display_server_wayland.cpp:95 (discriminator 2))
[48] DisplayServerWayland::dispatch_input_events(Ref<InputEvent> const&) (/home/jwoon/Documents/GodotTemplates/godot/platform/linuxbsd/wayland/display_server_wayland.cpp:89 (discriminator 1))
[49] Input::_parse_input_event_impl(Ref<InputEvent> const&, bool) (/home/jwoon/Documents/GodotTemplates/godot/./core/input/input.cpp:774)
[50] Input::flush_buffered_events() (/home/jwoon/Documents/GodotTemplates/godot/./core/input/input.cpp:1055)
[51] DisplayServerWayland::process_events() (/home/jwoon/Documents/GodotTemplates/godot/platform/linuxbsd/wayland/display_server_wayland.cpp:1254 (discriminator 1))
[52] OS_LinuxBSD::run() (/home/jwoon/Documents/GodotTemplates/godot/platform/linuxbsd/os_linuxbsd.cpp:958 (discriminator 1))
[53] /mnt/Forerunner/SteamLibrary/steamapps/common/Godot Engine/godot.x11.opt.tools.64(main+0x2b3) [0x55ed9299921c] (/home/jwoon/Documents/GodotTemplates/godot/platform/linuxbsd/godot_linuxbsd.cpp:85)
[54] /lib64/libc.so.6(+0x3248) [0x7fd01fd83248] (??:0)
[55] /lib64/libc.so.6(__libc_start_main+0x8b) [0x7fd01fd8330b] (??:0)
[56] /mnt/Forerunner/SteamLibrary/steamapps/common/Godot Engine/godot.x11.opt.tools.64(_start+0x25) [0x55ed92998ea5] (??:?)
-- END OF BACKTRACE --
================================================================

```

### Steps to reproduce

-place a light into a scene
-bake the lightmap once
-bake it again without first removing the lightmap data resource from the lightmapGI

### Minimal reproduction project (MRP)

N/A