Does the "clear" member of the Array class destroys the children?

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

Hello,

Godot version:
2.1.4, 3.0. Both official releases.

Info:
I have an array (Array class) of objects that are dynamically created in a script using “push_back” member of the Array class. All objects are inherited from Node class or inherited from the Node class.
Sometimes I need to clear the array.

Question:
Should I manually erase children from memory by calling “queue_free()” for each node or the “clear” member of the Array class does that anyway?

Cheers,
Radu

:bust_in_silhouette: Reply From: Zylann

Nodes are not reference-counted objects, so clearing the array will not free them. You should call queue_free before removing them from the array.

Thank you very much!

radubolovan | 2018-02-01 20:29

Are you sure?
I was pretty certain nodes, as most objects in godot, are referenced counted, meaning they would be destroyed if anything didn’t reference them.
Which in his case .clear would free them if they were not kept anywhere else or in the scene_tree.

Guilherme Furst | 2018-02-02 02:07

Nodes are not reference counted (they don’t inherit Reference). Their ownership model is based on the scene tree.
Resources, on the other hand, inherit Reference so if you clear an array of resources they can be freed automatically.

Zylann | 2018-02-02 02:08

:bust_in_silhouette: Reply From: hilfazer

clear() will not free your nodes. Free them manually.

	for n in arr:
		if not n.is_inside_tree():
			n.free()
	arr.clear()

In case you want to destroy nodes if they are inside SceneTree call queue_free() for them.

You can find memory monitor by clicking “Debugger” in “Output” panel and then clicking “Monitors” tab. Keep an eye on “Objects” variable.