Quick answer: A lambda used a local variable that held a node, and that node was freed before the lambda ran. Godot notices when it calls the lambda, prints this error and passes null in place of the node, so the next line of the lambda usually fails too. Fix the lifetime: connect a method of the node itself so the connection disappears with it, bind tweens to the node, or capture an instance ID and look the node up when the lambda runs.
The message names no script and no line, which makes it hard to act on. It comes from the engine’s lambda code, and it means a callback you created earlier has outlived something it refers to: a signal handler on a long-lived object, a timer or tween callback, a deferred call, a stored Callable. This guide explains what a capture is and what the index counts, why a lambda connection survives a freed node when a method connection does not, how to find the lambda responsible, and three ways to fix it. The message exists in Godot 4.2 and later; the behavior of 4.0 and 4.1 is noted below.
- The exact error
- What a capture is, and what index 0 counts
- Why the lambda outlives the node
- Where it turns up
- Find the lambda
- Fix 1: connect a method of the node that can be freed
- Fix 2: capture an ID and look the node up
- Fix 3: bind tweens to the node they act on
- How to confirm the fix
- What a report from players shows
- Related guides
- Frequently asked questions
The exact error
In a terminal or in the log file of an exported game:
ERROR: Lambda capture at index 0 was freed. Passed "null" instead.
at: call (modules/gdscript/gdscript_lambda_callable.cpp:108)
In the editor’s Debugger > Errors tab:
E 0:00:07:0145 call: Lambda capture at index 0 was freed. Passed "null" instead.
<C++ Source> modules/gdscript/gdscript_lambda_callable.cpp:108 @ call()
The line number in the C++ file differs between engine versions, and the index can be 1, 2 and so on. In a game run from the editor or exported with debug, a second error normally follows at once, from inside the lambda, and this one has your script in it:
SCRIPT ERROR: Cannot call method 'queue_free' on a null value.
at: <anonymous lambda> (res://spawner.gd:14)
Depending on what the lambda does with the variable, the second error can also be Invalid access to property or key 'position' on a base object of type 'Nil'. or Invalid call. Nonexistent function 'queue_free' in base 'Nil'. Treat the pair as one problem. The first line is the cause and the second line is the location.
Godot 4.0 and 4.1 do not print this message. There the lambda receives the freed object as it is, and the failure shows up inside the lambda as an error about a previously freed instance. The cause and the fixes are the same.
What a capture is, and what index 0 counts
A lambda can use local variables of the function that creates it, including that function’s parameters and for loop variables. Each one it uses is a capture. GDScript copies the captured values into the lambda once, at the moment the func expression runs. For a number or a string that copy is the value. For a node it is a reference to the node, and holding that reference does not keep the node alive.
func spawn_enemy() -> void:
var enemy: Enemy = enemy_scene.instantiate()
var marker: Sprite2D = marker_scene.instantiate()
add_child(enemy)
minimap.add_child(marker)
enemy.died.connect(func(): marker.queue_free()) # captures marker
Each time Godot calls a lambda, it first walks the list of captures. For every capture that holds an object it checks whether the object still exists. If it does not, Godot prints the error and hands the lambda null for that capture. Then the body runs. In the example, when the minimap is rebuilt and frees its markers, any enemy that dies afterwards calls the lambda with a dead marker.
Three details follow from how this works:
- The index is a position in that capture list, counted from zero. A lambda that uses one outer local variable has one capture, index 0. With several, the list is filled as the compiler meets each variable in the body, which is close to the order of first use but not guaranteed to match it. With more than one captured node, check each of them.
- Member variables and
selfare not captures. A lambda reads them through the script instance when it runs, so a member that has been freed gives a “previously freed” error, never this one. - Only objects you free by hand can trigger it. Nodes are the usual case. A captured
Resourceor otherRefCountedobject is kept alive by the capture.
The check happens before your code. That matters for the first fix people try, an is_instance_valid() test at the top of the lambda:
enemy.died.connect(func():
if is_instance_valid(marker):
marker.queue_free()
)
This stops the second error, because the body no longer touches null. It does not stop the first one. The engine has already printed “Lambda capture at index 0 was freed” by the time the if runs. To make the message go away, the lambda must not be called after the node is gone, or must not capture the node at all.
Why the lambda outlives the node
Godot ties every signal connection to one object, the object of the Callable, and removes the connection when that object is freed. Which object that is depends on how the Callable was written:
| What you connect | Connection is tied to | After marker is freed |
|---|---|---|
marker.queue_free (a method of the node) | marker | The connection is removed with it. Nothing is called |
func(): marker.queue_free() (uses only local variables) | No node. It stays until the emitter is freed or you disconnect it | The lambda still runs and reports the freed capture |
func(): hide_marker(marker) (also uses self or a member) | The node whose script created the lambda | The lambda still runs while that node lives, and reports the freed capture |
So a lambda connection is never tied to a captured node. A method connection is tied to exactly the node whose method it is. Most fixes below come down to moving from the second or third row to the first.
A tween callback or a deferred call is not a signal connection at all. It lives for as long as the tween or the engine’s call queue holds it. A one-off timer is a signal connection, but its emitter is a timer object owned by the scene tree, which nothing frees early.
Where it turns up
- A signal on a long-lived object, a lambda that captures a short-lived node. The example above. Autoload signals, a player’s signals and UI buttons that persist across levels are the common emitters.
- A loop that connects one lambda per item. A target list creates a button for each enemy and connects
func(): select(enemy)to itspressedsignal. The button outlives the enemy. - A one-off timer.
get_tree().create_timer(2.0).timeout.connect(func(): bullet.queue_free()), and the bullet hits something after one second. The timer belongs to the scene tree and fires regardless. - A tween callback.
tween.tween_callback(func(): popup.hide())on a tween created withget_tree().create_tween(), or created by a different node thanpopup. - A deferred or stored call. A lambda sent with
call_deferred(), or kept in an Array or Dictionary to run later (a command queue, a list of pending callbacks, a retry list).
Find the lambda
- Read the next error in the list. In a debug run it is the script error from inside the lambda, with a file and a line. That line is in the body of the lambda you are looking for.
- If lambdas show up as
<anonymous lambda>and you have several in one function, give them names. A named lambda is writtenfunc _remove_marker(): marker.queue_free(), and the debugger shows that name in place of the anonymous label. - If there is no second error (a release export, or a lambda that does not use the variable on that path), use Search > Find in Files for
func(and look at the hits next toconnect(,tween_callback(,create_timer(andcall_deferred(. For each, ask which outer local variables hold nodes and whether those nodes can be freed before the callback fires. - Note what happened in the game just before the message: a scene change, an enemy dying, a menu closing. The freed node is one that went away in that moment, and the lambda is one that was waiting on something else.
Callable.is_valid() does not help here. It reports on the lambda itself and returns true even when a capture has been freed.
Fix 1: connect a method of the node that can be freed
If the lambda only calls one method on the captured node, connect that method directly:
# Before
enemy.died.connect(func(): marker.queue_free())
get_tree().create_timer(2.0).timeout.connect(func(): bullet.queue_free())
# After
enemy.died.connect(marker.queue_free)
get_tree().create_timer(2.0).timeout.connect(bullet.queue_free)
Both connections now belong to the node that might be freed, and Godot removes them when it is. If the signal sends arguments the method does not take, drop them with unbind(): area.body_entered.connect(marker.queue_free.unbind(1)).
When the lambda does more than one thing, move the body into a method on the captured node’s script and connect that:
# marker.gd
func fade_out_and_free() -> void:
var tween := create_tween()
tween.tween_property(self, "modulate:a", 0.0, 0.3)
tween.tween_callback(queue_free)
# spawner.gd
enemy.died.connect(marker.fade_out_and_free)
Be careful with the halfway version, a method on your own script with the node passed through bind(): enemy.died.connect(_on_enemy_died.bind(marker)). That connection is tied to your script’s node, not to marker. If marker is freed first, the handler is still called and receives a freed object. It suits the case where the bound node cannot die before your node does.
You can also keep the lambda in a variable and disconnect it when the captured node leaves the tree. That needs a second callback on tree_exiting, which in turn must not capture a node that can be freed first. A method connection gives the same result with nothing to track.
Fix 2: capture an ID and look the node up
When the callback has to stay a lambda (it mixes several nodes, or it lives in a queue), capture something that cannot be freed. An instance ID is an integer:
var marker_id := marker.get_instance_id()
enemy.died.connect(func():
var m := instance_from_id(marker_id) as Sprite2D
if m:
m.queue_free()
)
instance_from_id() returns null when no object with that ID exists any more, so the lambda decides for itself what to do, and the engine has no object capture to complain about. A WeakRef does the same job and reads a little more clearly when you pass it around:
var marker_ref: WeakRef = weakref(marker)
pending_actions.append(func():
var m := marker_ref.get_ref() as Sprite2D
if m:
m.queue_free()
)
The same idea covers data. If the lambda only needs the enemy’s name or position at the time it was created, capture the String or the Vector2, not the enemy.
Fix 3: bind tweens to the node they act on
A tween is processed by the scene tree, not by the node that asked for it. get_tree().create_tween() gives you a tween with no owner, which keeps running after every node it mentions is gone. node.create_tween() gives you a tween bound to node: it is killed automatically when that node is freed, and its remaining callbacks never run.
# Broken: nothing stops this tween when popup is freed
var tween := get_tree().create_tween()
tween.tween_interval(1.5)
tween.tween_callback(func(): popup.hide())
# Fixed: created by, and bound to, the node the callback uses
var tween := popup.create_tween()
tween.tween_interval(1.5)
tween.tween_callback(popup.hide)
Calling create_tween() with no prefix binds the tween to the node running the script, which is correct when that node is the one being animated. When the script animates a different node, either call create_tween() on that node as above, or rebind with create_tween().bind_node(popup).
A SceneTreeTimer from get_tree().create_timer() has no such binding. Use the method connection from Fix 1, or put a Timer node under the node it serves so the two are freed together.
How to confirm the fix
Reproduce the order of events that caused it: create the callback, free the node, then let the callback’s trigger happen. For the minimap example that means spawning an enemy, rebuilding the minimap, then killing the enemy. Watch the Errors tab. With Fix 1 or Fix 3 nothing is called and nothing is printed. With Fix 2 the lambda runs and takes its null branch silently. If you only added is_instance_valid(), the script error is gone and the “Lambda capture” line remains, which tells you the capture is still there.
What a report from players shows
The capture line is written to the engine log in release exports as well as debug ones. Bugnet’s Godot SDK reads that log file for its automatic error capture, so with File Logging enabled (it is off by default on mobile exports) the line can arrive as a report with the platform, OS and game version. Every occurrence has the same text, whichever lambda caused it, so they all stack into one entry. The occurrence count tells you how often it happens and the reports tell you on which builds. They do not tell you which lambda.
That part depends on the build. A debug export logs the script error from inside the lambda right after the capture line, and the SDK attaches the surrounding log lines, so the file and line number are in the report. A release export leaves out GDScript’s null checks: there is no second line, and the lambda carries on with a null value, which can do nothing or can crash the game. A crash of that kind does not reach Bugnet as a stack trace. For callbacks where this matters, use Fix 2 and send the null branch yourself with report_error(message, stack), naming the callback in the message. The getting started guide covers setup.
Related guides
- How to Fix “Attempt to call function on a previously freed instance” in Godot
- How to Fix “Invalid access to property or key on a base object of type ‘Nil’” in Godot
- How to Fix “Attempt to disconnect a nonexistent connection” in Godot
- How to Fix a Godot Memory Leak from Undisconnected Signals
- How to Fix “Started with no Tweeners” Tween Errors in Godot 4
Frequently asked questions
What does Lambda capture at index 0 was freed mean in Godot?
A lambda used a local variable that held an object, usually a node, and that object was freed before the lambda was called. Godot passes null in its place, so the lambda body normally fails on the line that uses the variable.
What is index 0 in the lambda capture error?
It is the position of the variable in the lambda's list of captured outer variables, counted from zero. A lambda that uses one outer local variable has a single capture, index 0. With several captured nodes, check each one.
Why does is_instance_valid() inside the lambda not remove the error?
Godot checks the captures and prints the message before the lambda body starts. The check inside the body prevents the second error from using null, but the capture line has already been logged.
Does Godot disconnect a lambda when the node it uses is freed?
No, a lambda connection is never tied to a node it captured. It belongs to the script's own node only when the lambda uses self or a member variable. A method connection such as node.queue_free is removed when that node is freed.
Which Godot versions print this message?
Godot 4.2 and later. In Godot 4.0 and 4.1 the lambda receives the freed object unchanged and the failure appears inside the lambda as a previously freed instance error.
A lambda does not keep the nodes it captures alive, and its connection is not removed when they are freed. Connect a method of the node that can die, or capture an ID and look the node up when the callback runs.