Attempt to call function in base "previously freed instance"

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

So I’m have a conditional statement that looks like this:
if move_target && move_target.is_in_group(“player”):

move_target is a variable and can be null when there is no target, that is what the first part of the condition is for, since it will only check “move_target.is_in_group(“player”)” when move_target is not null.

However occationally I’ll get the error:
“Attempt to call function “is_in_group” in base “previously freed instance” on a null instance.”
What exactly does this mean? Is it when the node “move_target” has just gotten “freed” by queue_free()? And it that case isn’t that essentially equivelant to null and shouldn’t be return false from “if move_target”?
Am I misunderstanding this completely or am I right but there is a reason it returns true.

:bust_in_silhouette: Reply From: Sween123

Be aware that there’s difference in queuefree() and free(). That’s very tricky. queue_free() doesn’t really free a node, indeed in some way it removes the node, but the value of the node will not be null, it still exsits in your program, but is labeled as a freed instance. Try use the function free() instead(Rember not to call free() when a method is still running. For example, ////func die(): … free()//// wrong. Use this instead/////func die(): … call_deferred(“free”)/////)
Edit: Sorry above is wrong in some ways. Seems that queue_free() instance will be a null object.
Anyway:

func die(): 
    ........
    call_deferred("free")
:bust_in_silhouette: Reply From: Eric Ellingson

I would not recommend using free(). queue_free() does in fact remove the node, just not immediately. (it waits until it is safe to do so).

try:

if (move_target
    and weakref(move_target).get_ref()
    and move_target.is_in_group("player"):

    # do whatever

weakref(move_target).get_ref() checks to see if the node has been freed yet or not

Edit:weakref(move_target).get_ref() tried this, returning null for both, interesting. I got problems when using queue_free(). Still it’s confusing for me to have free() if they do the same thing while queue_free() is simply safer.

Sween123 | 2020-02-12 10:14