godot-docs icon indicating copy to clipboard operation
godot-docs copied to clipboard

Memory leak in draw_string example

Open TheDuriel opened this issue 3 years ago • 3 comments

Your Godot version: 3.stable

Issue description: The code example creates an orphan node.

The below is the corrected example which does not leak memory.

var control = Control.new()
var default_font = control.get_font("font")
control.queue_free()
draw_string(default_font, Vector2(64, 64), "Hello world")

URL to the documentation page: https://docs.godotengine.org/en/stable/classes/class_canvasitem.html#class-canvasitem-method-draw-string

TheDuriel avatar May 17 '22 20:05 TheDuriel

To avoid this kind of Control node creation (which I always found uncanny), I wonder if we should expose the default project font as a SceneTree property or getter method.

Calinou avatar May 17 '22 21:05 Calinou

Yeah. It's definitely relevant. I am working on a bunch of tool enabled nodes with editor only drawing, and my only option is this hack for every single, potentially hundreds of nodes. Or include a copy of my own font. There is no means to elegantly hook into the default theme / font for the engine for custom drawing.

Perhaps instead of a SceneTree property, the ProjectSettings.custom_font could point to the default one? https://docs.godotengine.org/en/stable/classes/class_projectsettings.html#class-projectsettings-property-gui-theme-custom-font

TheDuriel avatar May 17 '22 21:05 TheDuriel

Perhaps instead of a SceneTree property, the ProjectSettings.custom_font could point to the default one? docs.godotengine.org/en/stable/classes/class_projectsettings.html#class-projectsettings-property-gui-theme-custom-font

The project setting is only a String containing a path to a font, not an actual Font resource. Even though you can use load(), we preferably should have an actual Font resource here.

Calinou avatar May 17 '22 21:05 Calinou

In Godot 4, Window and Control's get_theme_default_font() and ThemeDB's fallback_font exist. The linked example brought in the report has also been updated not to use the leak-prone example.

Issue still persists in 3.x, though.

Mickeon avatar Dec 30 '23 17:12 Mickeon