godotengine / godotengine/godot

Resource reference leaks with weakref in tool scripts

Open
#115,448 3 comments 0 reactions 0 assignees View on GitHub
bug needs testing regression topic:editor
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

- Reproducible in 4.6
- Not Reproducible in 4.5.1

### System information

Godot v4.6.stable - Windows 11 (build 26100) - Multi-window, 2 monitors - OpenGL 3 (Compatibility) - AMD Radeon(TM) RX Vega 10 Graphics (Advanced Micro Devices, Inc.; 31.0.21923.11000) - AMD Ryzen 7 3700U with Radeon Vega Mobile Gfx (8 threads) - 5.94 GiB memory

### Issue description

WeakRefs still hold references to resources that should have been freed, when used in a tool script

### Steps to reproduce

Executing the following script (setting picture to the default icon)
```GDscript
extends Node2D

@export var picture: Texture2D:
set(value):
picture = value
if picture:
ref = weakref(picture)
if ref:
prints('weakref holds', ref.get_ref(), 'picture is', picture)
queue_redraw()
var ref: WeakRef

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
picture = null

func _draw() -> void:
if ref and ref.get_ref():
draw_texture(ref.get_ref(), Vector2.ZERO)
```
yields
```
weakref holds (res://icon.svg): picture is (res://icon.svg):
weakref holds picture is
```
Reloading the scene in tool mode, I get
```
weakref holds (res://icon.svg): picture is (res://icon.svg):
weakref holds (res://icon.svg): picture is
```
Setting the Texture in the inspector should also show the "picture" still being drawn even when reset to null

### Minimal reproduction project (MRP)

N/A

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied script in Godot 4.6.stable in tool mode and compare its output before and after resetting picture to null. Trace the resource and WeakRef lifetime in the engine; done when the weak reference no longer retains the texture and the node stops drawing it after reset.

Written by the indexing model from the issue text.

Assessment

Domain
game-dev, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.