godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.45k stars 21.27k forks source link

`IsInstanceValid` called before the end of the frame returns true on an object that called `QueueFree` #99239

Open IurieYSI opened 2 weeks ago

IurieYSI commented 2 weeks ago

Tested versions

tested on v4.3.stable.mono.official

System information

Windows 10 - Godot v4.3.stable.mono.official

Issue description

If we call node.QueueFree(), IsInstanceValid(node) should return false even before the end of the frame. I can't think of a reason why would anyone need to have IsInstanceValid(node) returning true after a node.QueueFree() call.

Steps to reproduce

  1. Create a node,
  2. Print out IsInstanceValid
  3. QueueFree the node
  4. Print out IsInstanceValid

It prints out

true
true

and I think it must print out

true
false

Minimal reproduction project (MRP)

ColorRect n = new ColorRect();
AddChild(n);
GD.Print(IsInstanceValid(n));
n.QueueFree();
GD.Print(IsInstanceValid(n));
AThousandShips commented 2 weeks ago

You can just use IsQueuedForDeletion to get that status

Blue-EyesWhiteDragon commented 2 weeks ago

It is queued to be freed from memory. "Unlike with Object.free, the node is not deleted instantly, and it can still be accessed before deletion. It is also safe to call queue_free multiple times. Use Object.is_queued_for_deletion to check if the node will be deleted at the end of the frame." - Godot Documentation

AThousandShips commented 2 weeks ago

There's a lot of behind the scenes code that depends on the behavior of this, or rather on what is_instance_valid checks, so to change this we would either have to make the bound function not work the same way or check for the queued status, or this would break a lot of internal code that is allowed to operate on the object until it is actually freed

That's exactly why queue_free exists, it's there to not break internal things that might operate on it before the end of the frame, otherwise you'd just use free