Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor crashes when AtlasTexture resource has C# script attached #57503

Closed
cgbeutler opened this issue Feb 1, 2022 · 1 comment · Fixed by #57738
Closed

Editor crashes when AtlasTexture resource has C# script attached #57503

cgbeutler opened this issue Feb 1, 2022 · 1 comment · Fixed by #57738

Comments

@cgbeutler
Copy link

Godot version

3.4.2 mono

System information

Windows 10

Issue description

I was creating an AtlasTexture derived script to allow for quick grid-based icon displays. If you then create an AtlasTexture in res and add the script to it, the editor will then crash when you next open it with no output as to the issue.

I had to compile the editor and attach a debugger to even narrow down that that was indeed the issue.
Exception Deets:

Exception thrown at 0x00007FFA3567D057 (mono-2.0-sgen.dll) in godot.windows.tools.64.mono.exe: 0xC0000005: Access violation reading location 0x00000000000000BC.

Call stack (without the unintelligible mono stuff.)

godot.windows.tools.64.mono.exe!GDMonoField::get_value(_MonoObject * p_object) Line 526
	at D:\repos\godot\modules\mono\mono_gd\gd_mono_field.cpp(526)
godot.windows.tools.64.mono.exe!CSharpScript::instance_create(Object * p_this) Line 3036
	at D:\repos\godot\modules\mono\csharp_script.cpp(3036)
godot.windows.tools.64.mono.exe!Object::set_script(const RefPtr & p_script) Line 1018
	at D:\repos\godot\core\object.cpp(1018)
godot.windows.tools.64.mono.exe!Object::set(const StringName & p_name, const Variant & p_value, bool * r_valid) Line 432
	at D:\repos\godot\core\object.cpp(432)
godot.windows.tools.64.mono.exe!ResourceInteractiveLoaderText::poll() Line 573
	at D:\repos\godot\scene\resources\resource_format_text.cpp(573)
godot.windows.tools.64.mono.exe!ResourceFormatLoader::load(const String & p_path, const String & p_original_path, Error * r_error) Line 193
	at D:\repos\godot\core\io\resource_loader.cpp(193)
godot.windows.tools.64.mono.exe!ResourceLoader::_load(const String & p_path, const String & p_original_path, const String & p_type_hint, bool p_no_cache, Error * r_error) Line 262
	at D:\repos\godot\core\io\resource_loader.cpp(262)
godot.windows.tools.64.mono.exe!ResourceLoader::load(const String & p_path, const String & p_type_hint, bool p_no_cache, Error * r_error) Line 376
	at D:\repos\godot\core\io\resource_loader.cpp(376)
godot.windows.tools.64.mono.exe!EditorResourcePreviewGenerator::generate_from_path(const String & p_path, const Vector2 & p_size) Line 63
	at D:\repos\godot\editor\editor_resource_preview.cpp(63)
godot.windows.tools.64.mono.exe!EditorResourcePreview::_generate_preview(Ref<ImageTexture> & r_texture, Ref<ImageTexture> & r_small_texture, const EditorResourcePreview::QueueItem & p_item, const String & cache_base) Line 162
	at D:\repos\godot\editor\editor_resource_preview.cpp(162)
godot.windows.tools.64.mono.exe!EditorResourcePreview::_thread() Line 261
	at D:\repos\godot\editor\editor_resource_preview.cpp(261)
godot.windows.tools.64.mono.exe!EditorResourcePreview::_thread_func(void * ud) Line 102
	at D:\repos\godot\editor\editor_resource_preview.cpp(102)
godot.windows.tools.64.mono.exe!Thread::callback(Thread * p_self, const Thread::Settings & p_settings, void(*)(void *) p_callback, void * p_userdata) Line 78
	at D:\repos\godot\core\os\thread.cpp(78)
[External Code]
godot.windows.tools.64.mono.exe!thread_start<unsigned int (__cdecl*)(void *),1>(void * const parameter) Line 97
	at minkernel\crts\ucrt\src\appcrt\startup\thread.cpp(97)
[External Code]

Steps to reproduce

  • Create a CSharp script that inherits from AtlasTexture. Can be simple as:
using Godot;
[Tool] public class CustomTexture : AtlasTexture
{
  private CustomTexture() {}
}
  • In the Godot editor, compile.
  • In the Godot file explorer, create a new blank AtlasTexture resource.
  • In the Godot inspector, drag the CustomTexture.cs onto the script field.
    (Editor is usually fine at this point. Can recompile and keep working with no errors. CustomTexture and its script will work as expected.)
  • Close Godot and re-open it. The editor will crash. (You may have to open a scene to get it to crash.)
  • You will get no info on the command line. If you delete the .mono folder and open the editor, it will once again run even after compiling. Once you restart, it will again crash.

Minimal reproduction project

bug_CustomTexture.zip

@cgbeutler cgbeutler changed the title Editor crashes when Texture resource has C# script Editor crashes when AtlasTexture resource has C# script attached Feb 1, 2022
@raulsntos
Copy link
Member

raulsntos commented Feb 6, 2022

It seems the crash happens when Godot instantiates the custom class (since it's a Tool), specifically when trying to get the value of the nativeName field:

StringName native_name = NATIVE_GDMONOCLASS_NAME(native);

return mono_field_get_value_object(mono_domain_get(), mono_field, p_object);

I don't know why this would crash since the field is there in the AtlasTexture class. I've also noticed the same crash with other resource types.

Update: It seems mono_get_domain is returning NULL so I guess that's it, but I don't know why because I'm pretty sure the domain is created way before these classes are instantiated 🤔

Update 2: Of course mono_get_domain returns NULL, because we are calling it without attaching the mono scope thread. I opened PR #57738 to fix this (3.x version: #57739).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants