godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.79k stars 21.13k forks source link

RESOLVED: GDNative: calling file->get_as_text() crashes on runtime #65733

Closed RhapsodyInGeek closed 2 years ago

RhapsodyInGeek commented 2 years ago

Godot version

3.5 Stable

System information

Windows 10

Issue description

EDIT: Fixed by erasing the cpp + header bindings on my disk, downloading the latest 3.5 bindings, and rebuilding them. Currently working again. Not sure what happened since I didn't touch any of the binding source files.

I'm attempting to read a JSON file through GDNative, but the game crashes on runtime. I recreated the code in GDScript and had no issues. It could be possible that I'm doing something wrong, but the fact that the exact code works in GDScript isn't convincing.

Steps to reproduce

Functional GDScript:

func _ready():
    var file:= File.new()
    file.open("res://music/music.json", File.READ)
    var file_data: String = file.get_as_text()
    print(file_data)

Non-functional GDNative:

void MusicManager::_ready()
{
    Ref<File> file = Ref<File>(File::_new());
    file->open("res://music/music.json", File::READ);
    String file_data = file->get_as_text();
    Godot::print(file_data);
}

Crashes upon calling get_as_text() when using the GDNative class. _ready() method is declared in _register_methods() and code functions upon omitting / replacing get_as_text().

Minimal reproduction project

No response

MGilleronFJ commented 2 years ago

How did you figure out it crashes in get_as_text? Did you put prints? Did you attach a C++ debugger and it stopped at the line with get_as_text? (careful depending on the debugger it might point the line after the offending line) Did you get a crash dump with the call stack mentionning get_as_text?

RhapsodyInGeek commented 2 years ago

I literally isolated the call. It's not a very large function, and it only crashes when including that call to get_as_text. I first encountered it when trying JSON::get_singleton()->parse(file->get_as_text()), and then attempted to run the same code in GDScript with success there. It only happens when calling it with GDNative.

RhapsodyInGeek commented 2 years ago

Resolved by erasing the cpp + header files from disk, redownloading the latest 3.5 cpp + header repos, and rebuilding them. Everything works again. Not sure what happened since I never touched the source files, but I'll take it.