Attempt to call function "hide" in base "previously freed instance" on a null instance

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hyperoman

I add sprites that are needed to hide in the toDelete array, deleting them in _process, when trying to delete it outputs an error “Attempt to call function “hide” in base “previously freed instance” on a null instance”

for i in range(0,MAIN.toDelete.size()):
		MAIN.toDelete[i].hide()

You probably need to show all relevant code to get some input.

jgodfrey | 2022-12-08 17:38

In the button click function, I add this button to the array, and in the _process function I hide the buttons that are in the array

   func _on_Button1_1_pressed():
    	var spr = get_node("Button1_1")
    	MAIN.toDelete.append(spr)

func _process(delta):
	if update == -1:
		get_tree().reload_current_scene()
		update = 1;
		
	for i in range(0,MAIN.toDelete.size()):
		MAIN.toDelete[i].hide()

hyperoman | 2022-12-08 18:53

:bust_in_silhouette: Reply From: exuin

First, you shouldn’t be trying to hide the nodes every frame. Just call the function when you need to hide the nodes.

Second, at some point in the code you delete the node. This doesn’t delete the reference to the node in the array though so you get the error. You should remove the node from the array when you delete it.