godot icon indicating copy to clipboard operation
godot copied to clipboard

create_instance is called infinitely

Open JekSun97 opened this issue 1 year ago • 4 comments

Tested versions

4.3 stable

System information

Godot v4.3.stable - Windows 10.0.19045 - GLES3 (Compatibility) - Radeon RX 560 Series (Advanced Micro Devices, Inc.; 31.0.14001.45012) - Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz (4 Threads)

Issue description

When I call OS.create_instance function, it opens new instances of the project infinitely and causes OS memory leak.

BE CAREFUL WHEN RUNNING THE CODE AS THIS CAN REALLY CAUSE UNPREDICTABLE CONSEQUENCES!

extends Node
var a

func _ready() -> void:
	a = OS.create_instance([])
	pass
	
func _input(event: InputEvent) -> void:
	if event is InputEventKey:
		if event.pressed and event.keycode==KEY_A:
			if a!=null:
				OS.kill(a); a=null;

	pass

Steps to reproduce

Copy the code and run the project

Minimal reproduction project (MRP)

N/A

JekSun97 avatar Sep 30 '24 17:09 JekSun97

I don't think this is a godot issue, you're calling OS.create_instance inside the _ready function which will imediatly create another instance that will also create another and go on. This is a user error and i don't see what godot can do about, for use something like that you would need to control the amount of instances using a external file like that:

extends Node2D

var _path := "C:/Users/Matheus/Downloads/Godot MRP/test.cgf"


func _ready() -> void:
	var file := ConfigFile.new()
	
	if FileAccess.file_exists(_path):
		var error := file.load(_path)
		
		if error == OK:
			var current_value = file.get_value("a", "b", 9)
			if current_value <= 5:
				file.set_value("a", "b", current_value + 1)
				OS.create_instance([])
	
	else:
		file.set_value("a", "b", 1)
		OS.create_instance([])
	
	file.save(_path)

matheusmdx avatar Oct 01 '24 10:10 matheusmdx

Perhaps this behavior should be described in the documentation

JekSun97 avatar Oct 02 '24 06:10 JekSun97

you would need to control the amount of instances using a external file

You can also pass arguments to the process, so you can check the values of the args to decide whether to create a new instance or not.

RedMser avatar Oct 04 '24 15:10 RedMser

I struggle to see any valid reason to call this method in things like _ready, so this specific situation seems very unlikely to be something people would run into, I don't think an explanation for how to avoid infinite recursion here is relevant for the documentation, but if someone can present some valid case where you'd need this kind of check we can add that

I.e. when would you have an unconditional spawning of an instance on the loading of a node, and how would this be different from adding a child on ready which adds a child itself etc.

AThousandShips avatar Oct 04 '24 15:10 AThousandShips