How to tell if a Node3D is disposed 'Cannot access a disposed object. Object name: 'Godot.Node3D'.'

Godot Version

4.2.1

Question

I add objects async and then call add child on the process thread. It looks like there are race conditions under which the item to add already got disposed. Is there a way to see if a Node3D is already disposed?

public ConcurrentQueue<Node3D> NodesToAdd { get; set; } = new ConcurrentQueue<Node3D>();
public override void _Process(double delta)
    {
        while (NodesToAdd.TryDequeue(out var node))
            AddChild(node);
    }

full info to help searches.
System.ObjectDisposedException
HResult=0x80131622
Message=Cannot access a disposed object.
Object name: ‘Godot.Node3D’.
Source=GodotSharp
StackTrace:
at Godot.GodotObject.GetPtr(GodotObject instance) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs:line 75

is_instance_valid
public override void _Process(double delta)
    {
        while (NodesToAdd.TryDequeue(out var node))
            if(IsInstanceValid(node))
                AddChild(node);
    }

ok, that seems to work, I was looking for something on the object. We’ll see if I can get it to trigger again.

1 Like

in case this helps:

using Godot;
using System;
public partial class MyNode:Node2D {
	public override void _Ready() {
		GD.Print(Name);
		GD.Print(GetParent().Name);
	}
}
public partial class node_2d:Node2D {
	public override void _Ready() {
		Name = "I am the parent";
		MyNode v_child = new MyNode();
		v_child.Name = "I am the child";
		CallDeferred("add_child", v_child);
	}
}