Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak for the objects storing self reference #82882

Closed
giddycodedev opened this issue Oct 5, 2023 · 2 comments · Fixed by #82942
Closed

Memory leak for the objects storing self reference #82882

giddycodedev opened this issue Oct 5, 2023 · 2 comments · Fixed by #82942

Comments

@giddycodedev
Copy link

Godot version

v4.1.1.stable.official.bd6af8e0e

System information

Godot v4.1.1.stable - Windows 10.0.22621 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4080 (NVIDIA; 31.0.15.3623) - 13th Gen Intel(R) Core(TM) i9-13900K (32 Threads)

Issue description

Hi,
I have created custom class extending RefCounted type. Mentioned class does have a variable storing the reference to itself.

class_name Mem
extends RefCounted


var a := "a"
var b : Mem


func _init() -> void:
    b = self

When I create an object of that class in _process i can see the number of objects growing.

func _process(delta: float) -> void:
    var mem : Mem
    var memfine : MemFine
    print("OBJ COUNT BEFORE: ",Performance.get_monitor(Performance.OBJECT_COUNT))
    # Self referencing triggers memory leak
    mem = Mem.new()
    #memfine = MemFine.new()
    print("OBJ COUNT AFTER : ",Performance.get_monitor(Performance.OBJECT_COUNT))

I would expect Godot to clear the references and keep the number of objects at the same level.

Steps to reproduce

Run the scene from attached project and observe how object count is growing

Minimal reproduction project

MemLeak.zip

@giddycodedev giddycodedev changed the title Memory leak for objects storing the self reference Memory leak for the objects storing self reference Oct 5, 2023
@dalexeev
Copy link
Member

dalexeev commented Oct 6, 2023

I think this is intentional, stored self references are no different than other ones. Use weakref() or don't store the reference in a variable.

@giddycodedev
Copy link
Author

I see,
If this is an expected behavior of reference counting GC, maybe would be good to mention in official doc the fact that self-referential constructs are prone to memory leaks. If we feel this is not needed we can close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants