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 Windows

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.

extends Node

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

What Godot 4.7.2 prints

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.

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

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

freed: true

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](https://github.com/godotengine/godot/blob/4.7.2-stable/core/object/object.cpp#L781), [gdscript_vm.cpp line 2000, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L2000).

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.