Trying to cast a freed object in Godot 4
By Eduardo Orellana · Updated
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.
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.
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>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](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1637).
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.
Related
- Left operand of 'is' is a previously freed instance
- Cannot call method on a previously freed instance
- Every verified Godot 4 answer
- Invalid access to property or key on a base object of type 'previously freed' in Godot 4
- Invalid cast: can't convert a non-object value to an object type in Godot 4
- Cannot call method '…' on a null value in Godot 4
- Attempted to free a RefCounted object in Godot 4
- Trying to assign a non-object value to a variable of type '…' in Godot 4
Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.
Download Flockbay free