How to know a node is freed (or deleted)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kevin
:warning: Old Version Published before Godot 3 was released.

I call “node.free()”, but if only the node is freed, it throws an error
“Attempt to call function ‘free’ in base ‘previously freed instance’ on a null instance.”

How do I know if the node is deleted ? It shows “[Deleted Object]” when I use “print(node)” ,

Keep in mind that there is the functionqueue_free that will solve that problem in many cases.

Warlaan | 2016-11-21 11:28

But I have tried “queue_free”, it doesn’t work.

Kevin | 2016-11-23 02:11

That’s why I said “in many cases”. It can only solve the problem if you aren’t keeping and using a reference to the freed node yourself.
If you are re-acquiring it every time queue_free() should make sure that you can’t find a node that isn’t valid anymore.

(Which is not to say that this was a better solution in every case, it’s just something to keep in mind.)

Warlaan | 2016-11-23 09:05

:bust_in_silhouette: Reply From: volzhs
var wr = weakref(node)
if (!wr.get_ref()):
    # freed
else:
    # not freed

it works great!!!thanks

vipsbpig | 2017-06-15 06:48

This feels very wrong to me, as it seems to be a relying on an unreliable runtime safety check. What if another node took place of the deleted node in memory? Then I would assume get_ref would still succeed, and the OP would free a different node that is not the intended target. I don’t know Godot’s design well enough to be sure, but I don’t understand how it can provide reliable information about a deleted node without keeping a history of all deleted nodes, which I’m sure Godot is not doing (at least not in exported code, as the code would then consume all available memory over time and run out of memory). Maybe I’m missing something here. One should keep track of whether a node has been freed via other means, and not attempt to do any operation a freed node (and that includes weakref). If someone who knows Godot’s design better could chime in, that would be great.

ArdaE | 2018-11-18 05:20

By the way, this should work if the weakref was created before there was any chance the node was freed. That should be clearly specified in the answer though, as I can’t see how the code would work reliably the way it’s written.

ArdaE | 2018-11-18 06:00

Heh, I can tell you come from the land of C and know a bit about malloc. Remember, gdscript is a scripting language. An object never dies unless there are no more references to that object, this is a mandatory requirement. So, being able to even call weakref, means that “node” still exists in some sense of the word. You may have done “node = k”, and some other thread called “k.deallocate(); k = null”, but that other thread can’t make you lose the validity of “node” as an object, because that would violate the idea of a scripting a language. The main idea here, is that “node”/“k” will likely only contain an object_id, and then a pointer to the rest of the data. that pointer will be set to null “k.deallocate” is called, so that now “node” refers to a “deallocated node”, but it still “exists”, just as lonely null pointer and its old object_id. And, the 8 bytes used to keep track of the obj_id and the pointer will be deallocated whenever the variable “node” gets updated to reference something else, and the garbage collector cleans it up because neither “node” nor “k” reference it anymore. Regardless of weakref, it would be unacceptable to call some function on a node and then it magically calls the function on a different node instead because the node was deallocated and then reallocated. In summary, “node.queue_free()” will make the node and invalid node, but not destroy it completely.

Nicholas Pipitone | 2019-10-05 18:06

:bust_in_silhouette: Reply From: kiwibonga

As of Godot 3.1, you can use:

is_instance_valid(node)

Thank you this is exactly what i was looking for

David Thornton | 2019-07-02 05:45