Random problem of Object Disposed

Godot Version

4.5 rc1

Question

Sometimes it happens that, randomly, an error of Object Disposed occurs.
Currently I have a Dungeon generated procedurally, and, at the end, a Signal is raised and some Spawn Point are activated because in listening.
The error occurs during the AddChild method.
I use C#.
This is the trace of the error.

What can i do?

Looks like you have a breakpoint in your code and the error is when the code tries to stop at that br4eakpoint, the object being referenced is no longer there.

Hi, no breakpoint present.

Can you give us some of the code around line 47 of SpawnPoint.cs so we can see what’s triggering it?

Here the code:


(2) The row 47 is the row where the instatiated node is added as child to the current node.
The function is called when the Dungeon is generated (1).
As I mentioned, the error occurs randomly, I mean: I run the code 10 times (for instance) without error, sometimes it occurs the error traced in my initial post.

I haven’t done much with C#, but “Can’t access disposed object” implies to me that the object has been marked for deletion but not deleted yet. I think that’s why it’s getting past your null check; the thing isn’t null, but it’s on the way to being garbage collected.

I think there’s an isDisposed() check? Or maybe it’s is_disposed()?

EDIT: GetFirstNodeInGroup() seems like it could cause problems.

About the isDisposed check I didn’t find anything, the only check I found is “IsInstanceValid”, but despite this, the error occurs.
@hexgrid what do you mean with the last sentence:
“GetFirstNodeInGroup() seems like it could cause problems”

Why are you checking if a node you just created is null? Like what prompted that? It shouldn’t be null because you just created it.

You’ve got a "DungeonGenerator" group and you’re getting the first node in that. Is there more than one? I’m leery of GetFirstNodeInGroup() because if there’s more than one node in the group, it might not be in the order you think it is. In particular, there could be an old, dying dungeon generator in there that’s marked for deletion, and it could be first in the list.

No only one DungeonGenerator

You might want to make sure; maybe stick a test in there to make sure there’s only one element in the group, just while you’re debugging this.

If it’s not that, maybe SpawnPoint is going out of scope?

Yes I confirm that is the SpawnPoint that goes “out of scope”. I tried to change the node where the instantiated object is added with AddChild, with the node3d of the dungeon instead… The code works well where AddChild but it gives error when reach the next line…


Now I need to understand why the spawnpoint goes “out of scope”…

I eliminated the management through the Signal raised by the DungeonGenerator and, in place of this, I retrieve all the SpawnPoint node at the end of the dungeon generator and after call the Spawn function in each of them… all everything work.