Trying to cast a freed object in Godot 4
The variable points at an object that was freed, and as cannot cast what no longer exists. enemy as Node after enemy.free() stops with Trying to cast a freed object. Check is_instance_valid(enemy) before the cast.
AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.
Intel or AMD Chromebook, with Linux turned on.
What Godot is checking
A cast to a class looks up the object to read its class. Freeing an object leaves variables holding a reference to nothing, and the VM stops with this message rather than returning null.
queue_free() frees at the end of the frame, so a reference kept until the next frame is in the same state.
More on this: Invalid access to property or key on a base object of type 'previously freed' in Godot 4
Reproduce it
A node freed, then cast with as.
More on this: Attempted to free a RefCounted object in Godot 4
extends Node
func _ready() -> void:
var enemy = Node.new()
enemy.free()
var target = enemy as Node
print("after")
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Trying to cast a freed object.
at: _ready (res://answers/trying-to-cast-a-freed-object/repro.gd:6)
GDScript backtrace (most recent call first):
[0] _ready (res://answers/trying-to-cast-a-freed-object/repro.gd:6)Fix it
Cast only an object that still exists.
More on this: Invalid cast: can't convert a non-object value to an object type in Godot 4
extends Node
func _ready() -> void:
var enemy = Node.new()
enemy.free()
var target: Node = enemy as Node if is_instance_valid(enemy) else null
print("target: ", target)
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
target: <null>Run this fix with your agent
Flockbay is the Godot editor with an AI agent built in. Paste this into Flockbay's chat, or into Claude Code or Codex connected to Flockbay over MCP: the agent finds the cause in your project, applies the fix, runs the game headless and reads Godot's output to check it.
More on this: Reference in Godot 4: it is RefCounted now
My Godot 4 project prints this error: "Trying to cast a freed object.". Find where it comes from and fix it. The variable points at an object that was freed, and as cannot cast what no longer exists. enemy as Node after enemy.free() stops with Trying to cast a freed object. Check is_instance_valid(enemy) before the cast. Then run the game headless and check that Godot's output no longer shows it.Verified on Godot 4.7.2
Both scripts on this page were run headless on Godot 4.7.2 (4.7.2.stable.mono.official.ed1daf0bf) on 18 October 2026, and the output above is what Godot printed, unedited.
Where the check lives in Godot's source: gdscript_vm.cpp line 1637, the message.
Questions
- Is a freed object null?
- No. Its variables still hold a reference, and comparing it to null on 4.7.2 is true, but casting it or calling it fails. is_instance_valid() is the check that covers both.
- How do I stop keeping references to freed enemies?
- Remove them from your arrays when they die, or connect to the node's tree_exiting signal and drop the reference there.
Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.
Download Flockbay free