# Attempted to free a RefCounted object in Godot 4

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.

Source: https://flockbay.com/godot/attempted-to-free-a-refcounted-object/ · Updated 2026-10-30

## 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
```

## 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.

```
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](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.

Download Flockbay free: Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot. https://flockbay.com/download/

Related: [Reference in Godot 4: it is RefCounted now](/godot/reference-in-godot-4/), [Trying to cast a freed object in Godot 4](/godot/trying-to-cast-a-freed-object/), [Cannot call method '…' on a previously freed instance in Godot 4](/godot/cannot-call-method-on-a-previously-freed-instance/), [Left operand of 'is' is a previously freed instance in Godot 4](/godot/left-operand-of-is-is-a-previously-freed-instance/), [Invalid access to property or key on a base object of type 'previously freed' in Godot 4](/godot/invalid-access-to-property-or-key-on-previously-freed/), [Every verified Godot 4 answer](/godot/), [How do I see Godot’s output and errors in Flockbay?](/docs/how-do-i-see-godots-output-in-flockbay/)
