godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.09k stars 21.18k forks source link

Game crashes on exit if using CanvasItem string drawing functions #98594

Open EricPendergast opened 2 weeks ago

EricPendergast commented 2 weeks ago

Tested versions

System information

Godot v4.3.stable.mono - macOS 15.0.1 - Vulkan (Forward+) - integrated Apple M2 Pro - Apple M2 Pro (12 Threads)

Issue description

When running the scene with the attached script, it works fine while its running, however when closing the game it hangs indefinitely. In the editor, there is nothing indicating an error. In VSCode, it shows the following backtrace:

================================================================
handle_crash: Program crashed with signal 11
Engine version: Godot Engine v4.3.stable.mono.official (77dcf97d82cbfe4e4615475fa52ca03da645dbd8)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] invoke_previous_action(sigaction*, int, __siginfo*, void*, bool)
[2] 2   libsystem_platform.dylib            0x000000018d890184 _sigtramp + 56
[3] 3   libc++abi.dylib                     0x000000018d811fec __dynamic_cast + 56
[4] RefCounted::unreference() (in Godot) + 196
[5] Theme::~Theme() (in Godot) + 764
[6] ThemeDB::~ThemeDB() (in Godot) + 84
[7] Main::cleanup(bool) (in Godot) + 776
[8] main (in Godot) + 408
[9] 9   dyld                                0x000000018d4d8274 start + 2840
-- END OF BACKTRACE --
================================================================

The script in question is as follows:

using Godot;
using System;

public partial class DrawingBug : Node2D
{
    public override void _Draw() {
        var screenPos = new Vector2I(100, 100);
        var text = "Hello";
        DrawString(ThemeDB.FallbackFont, screenPos, text,
            HorizontalAlignment.Left, -1, 14, Colors.White);
    }
}

This happens with all of the string drawing functions (DrawStringOutline, DrawMultilineString, DrawMultilineStringOutline) I suspect this has something to do with the order of font usage and cleanup.

Steps to reproduce

  1. Open the attached project
  2. Run bug.tscn (contains just a Node2D with the above script attached)
  3. Click the "x" to close the window

Minimal reproduction project (MRP)

drawbug.zip

EricPendergast commented 2 weeks ago

This issue actually has a bit larger scope than I thought, as the crash happens even if the object has been long destroyed:

    public override void _Draw() {
        var screenPos = new Vector2I(100, 100);
        var text = "Hello";
        DrawString(ThemeDB.FallbackFont, screenPos, text,
            HorizontalAlignment.Left, -1, 14, Colors.White);
        // Crash still happens despite draw call not running at quit time
        this.QueueFree();
    }
EricPendergast commented 2 weeks ago

A workaround for this issue is to use a custom font instead of the ThemeDB.FallbackFont

bruvzg commented 2 weeks ago

Not reproducible with current master.