+1 vote

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

in Engine by (104 points)

2 Answers

+2 votes
Best answer

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.

by (2,256 points)
selected by
+1 vote

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.

by (27,774 points)

Thank you very much!

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.