godot: Memory leak with circular reference in GDscript

Hi,

Godot reference counting mechanism doesn’t support circular references. This is fine for simple construction (e.g. loading References) but given GDscript is build on this, you can write leaking program pretty easily:

func _ready():
	set_process(true)

class Leaker:
	var stuff = null
	var other = null
	func _init():
		# Dummy data to make the leak more visible
		self.stuff = []
		for x in range(1000):
			self.stuff.append(x)

	func connect(other):
		self.other = other

func _process(delta):
	var a = Leaker.new()
	var first = a
	for i in range(100):
		var b = Leaker.new()
		a.connect(b)
		a = b
	a.connect(first)  # Comment this line to remove the leak !

This is not a trouble for me (beside a circular dependency checker is complex to code and slow to run) but I think it’s something users should be aware of. I couldn’t see any information about this behavior in the documentation so I think there should be a warning about this in GDscript more efficiently

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Reactions: 6
  • Comments: 17 (16 by maintainers)

Most upvoted comments

@YuriSizov Unfortunately, I just tested and it still leaks. I’m investigating this.

@Noshyaar I’ve just reproduce the leak with Godot 3.0.2, so this issue is still valid

There’s weakref() but not much info about it.