Godot Error FATAL: Condition "!Object::cast_to<Reference>(p_ptr)" is true

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bastagedgak

Hello. I am getting this fatal error when testing my game. I am using the mono version of Godot, I am doing everything with C# scripts.

ERROR: godot_icall_Reference_Disposed: FATAL: Condition "!Object::cast_to<Refere
nce>(p_ptr)" is true.
   At: modules/mono/glue/base_object_glue.cpp:85

=================================================================
        Native Crash Reporting
=================================================================
Got a UNKNOWN while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================

=================================================================
        Managed Stacktrace:
=================================================================
          at <unknown> <0xffffffff>
          at Godot.Object:godot_icall_Reference_Disposed <0x000c4>
          at Godot.Object:Dispose <0x001e2>
          at Godot.Object:Finalize <0x000a4>
          at System.Object:runtime_invoke_virtual_void__this__ <0x00184>
=================================================================

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
ERROR: Resource file not found: res://.
   At: core/io/resource_loader.cpp:287

It happens quite frequently, but not consistently at all – I have a save system implemented and whenever I reload a save after a crash, the crash will likely not happen at the same place. Could anyone help me understand what could I be doing wrong? Why is there no real C# stack trace? Thank you very much.

Have you fixed this issue? If not, can you provide us the part of your source code?

juppi | 2021-11-06 15:16

:bust_in_silhouette: Reply From: Agusdel

This is an old question, but had the same crash and couldn’t find a solution anywhere. This was the only thread even mentioning it and there was no answer. I was able to figure out the cause after a few hours of debugging, so I wanted to share it in case anyone has the same issue in the future.

In my case, this exact crash was happening shortly after destroying a very specific node in game: a weapon when the player unequipped it.

The reason why it was happening is because I was calling MemberwiseClone() on the object storing the information of my weapon, including the PackedScene that I was instancing.

public class ObjectData : Resource
{
	[Export]
	public string id;

	[Export]
	public string name;

	[Export]
	public PackedScene scene;

	public ObjectData Clone()
	{
		return (ObjectData)this.MemberwiseClone();
	}
}

I’m no expect in C#, so I don’t know exactly what MemberwiseClone() does under the hood, but long story short, the copy of the PackedScene is invalid in some way, and even if creating instances of it will work just fine, you get the crash described shortly after freeing them.

Once I replaced the implementation of Clone() with the following, the game stopped crashing and everything worked as intended.

public ObjectData Clone()
{
    	ObjectData clone = (ObjectData)this.MemberwiseClone();
    	// Keep the original reference to scene
    	clone.scene = scene;
    	return clone;
 }

Note that I’m not suggesting the code above as a fix. That code is just for reference, to make it clear that the new reference to the PackedScene created by MemberwiseClone() was causing the issue.

Also, MemberwiseClone() is one way to reproduce, but that function is probably doing something generic, so I’m sure this can be reproduced by creating copies of a PackedScene in different ways. If you are not using that function but still experience this crash, I’d suggest looking for places where you create copies ofPackedScene in any way other than using the = operator.