Skip to content

Attempted to free a RefCounted object in Godot 4

By Eduardo Orellana · Updated

RefCounted objects, Resources included, are freed automatically when nothing refers to them, and free() is refused on them. r.free() on a RefCounted.new() stops with Can't free a RefCounted object and Attempted to free a RefCounted object. Drop the reference instead: set the variable to null, or let it go out of scope.

AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.

Intel or AMD Chromebook, with Linux turned on.

Download for macOSDownload for WindowsDownload for Linux

What Godot is checking

Object.free() checks whether the object is RefCounted and refuses, printing Can't free a RefCounted object from object.cpp. The GDScript VM then reports the failed call as Attempted to free a RefCounted object.

A RefCounted counts the references to it. When the last variable, array slot or property holding it lets go, it frees itself, so there is nothing to call.

Reproduce it

A RefCounted created and then freed by hand.

More on this: Left operand of 'is' is a previously freed instance in Godot 4

GDScript
extends Node

func _ready() -> void:
	var data = RefCounted.new()
	data.free()
	print("after")

What Godot 4.7.2 prints

Output
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

ERROR: Can't free a RefCounted object.
   at: callp (core/object/object.cpp:781)
   GDScript backtrace (most recent call first):
       [0] _ready (res://answers/attempted-to-free-a-refcounted-object/repro.gd:5)
SCRIPT ERROR: Attempted to free a RefCounted object.
   at: _ready (res://answers/attempted-to-free-a-refcounted-object/repro.gd:5)
   GDScript backtrace (most recent call first):
       [0] _ready (res://answers/attempted-to-free-a-refcounted-object/repro.gd:5)

Fix it

Release the reference; the object frees itself when the count reaches zero.

GDScript
extends Node

func _ready() -> void:
	var data = RefCounted.new()
	var ref: WeakRef = weakref(data)
	data = null
	print("freed: ", ref.get_ref() == null)

What the fixed script prints

Output
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

freed: true

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

Prompt
My Godot 4 project prints this error: "Attempted to free a RefCounted object.". Find where it comes from and fix it. RefCounted objects, Resources included, are freed automatically when nothing refers to them, and free() is refused on them. r.free() on a RefCounted.new() stops with Can't free a RefCounted object and Attempted to free a RefCounted object. Drop the reference instead: set the variable to null, or let it go out of scope. 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: object.cpp line 781, the engine refusal, gdscript_vm.cpp line 2000, the message.

Questions

Which classes are RefCounted?
Resource and everything under it (textures, scenes as PackedScene, materials, custom Resources), plus classes such as RegEx, JSON, Tween and FileAccess. Nodes are not: free them with queue_free().
How do I free a Resource I loaded?
Let every reference go. A Resource loaded with load() is also kept by the resource cache while anything uses it.

Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.

Download Flockbay free