Repository navigation
Closed
Labels
bugThis has been identified as a bug
Description
Version: Godot Engine v4.0.beta16.official.518b9e580
MSVC 19.33.31630 for x64
When passing an instance of an Object with type hint as a parameter to a GDExtension function the running game will crash. This issue is only present when using the type hint for the object variable and for the instance the function is being called on. If the type hint is not provided in one of the two cases, it will not cause a crash. The crash can be reproduced by following this minimal example:
- Create two example classes in your GDExtension, one called "ExampleObject" that inherits from "Object" and another called "Example" that inherits from "RefCounted":
class ExampleObject : public Object
{
GDCLASS(ExampleObject, Object);
protected:
static void _bind_methods()
{
}
public:
String test = "hello world";
};
class Example : public RefCounted
{
GDCLASS(Example, RefCounted);
protected:
static void _bind_methods()
{
ClassDB::bind_method(D_METHOD("do_something", "object"), &Example::do_something);
}
public:
void do_something(ExampleObject* example_object)
{
UtilityFunctions::print(example_object->test);
}
};- In GDScript, create an instance of the "ExampleObject" class with a type hint and pass it as a parameter to the "do_something" method of an instance of the "Example" class:
func _enter_tree():
var example:Example = Example.new()
var object:ExampleObject = ExampleObject.new()
example.do_something(object)
- Crash.
Note: If the type hint for the object variable or the initial example class insstance is not provided, the engine does not crash:
func _enter_tree():
var example:Example = Example.new()
var object = ExampleObject.new()
example.do_something(object)
# prints "hello world"func _enter_tree():
var example = Example.new()
var object:ExampleObject = ExampleObject.new()
example.do_something(object)
# prints "hello world"Stack trace:
> godot.windows.editor.dev.x86_64.exe!mtx_do_lock(_Mtx_internal_imp_t * mtx, const xtime * target) Line 102 C++
godot.windows.editor.dev.x86_64.exe!std::_Mutex_base::lock() Line 50 C++
godot.windows.editor.dev.x86_64.exe!Object::get_instance_binding(void * p_token, const GDExtensionInstanceBindingCallbacks * p_callbacks) Line 1699 C++
godot.windows.editor.dev.x86_64.exe!gdextension_object_get_instance_binding(void * p_object, void * p_token, const GDExtensionInstanceBindingCallbacks * p_callbacks) Line 907 C++
libfmod.windows.template_debug.dll!godot::PtrToArg<ExampleObject *>::convert(const void * p_ptr) Line 172 C++
libfmod.windows.template_debug.dll!godot::call_with_ptr_args_helper<Example,ExampleObject *,0>(Example * p_instance, void(Example::*)(ExampleObject *) p_method, const void * const * p_args, IndexSequence<0> __formal) Line 194 C++
libfmod.windows.template_debug.dll!godot::call_with_ptr_args<Example,ExampleObject *>(Example * p_instance, void(Example::*)(ExampleObject *) p_method, const void * const * p_args, void * __formal) Line 215 C++
libfmod.windows.template_debug.dll!godot::MethodBindT<Example,ExampleObject *>::ptrcall(void * p_instance, const void * const * p_args, void * r_ret) Line 333 C++
libfmod.windows.template_debug.dll!godot::MethodBind::bind_ptrcall(void * p_method_userdata, void * p_instance, const void * const * p_args, void * r_return) Line 105 C++
godot.windows.editor.dev.x86_64.exe!GDExtensionMethodBind::ptrcall(Object * p_object, const void * * p_args, void * r_ret) Line 198 C++
godot.windows.editor.dev.x86_64.exe!GDScriptFunction::call(GDScriptInstance * p_instance, const Variant * * p_args, int p_argcount, Callable::CallError & r_err, GDScriptFunction::CallState * p_state) Line 2000 C++
godot.windows.editor.dev.x86_64.exe!GDScriptInstance::callp(const StringName & p_method, const Variant * * p_args, int p_argcount, Callable::CallError & r_error) Line 1842 C++
godot.windows.editor.dev.x86_64.exe!Node::_gdvirtual__enter_tree_call<0>() Line 240 C++
godot.windows.editor.dev.x86_64.exe!Node::_propagate_enter_tree() Line 218 C++
godot.windows.editor.dev.x86_64.exe!Node::_propagate_enter_tree() Line 235 C++
godot.windows.editor.dev.x86_64.exe!Node::_set_tree(SceneTree * p_tree) Line 2595 C++
godot.windows.editor.dev.x86_64.exe!SceneTree::initialize() Line 411 C++
godot.windows.editor.dev.x86_64.exe!OS_Windows::run() Line 1294 C++
godot.windows.editor.dev.x86_64.exe!widechar_main(int argc, wchar_t * * argv) Line 181 C++
godot.windows.editor.dev.x86_64.exe!_main() Line 203 C++
godot.windows.editor.dev.x86_64.exe!main(int argc, char * * argv) Line 217 C++
godot.windows.editor.dev.x86_64.exe!WinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 231 C++
In the example project of this repo you can do this to trigger the crash:
var example:Example = Example.new()
var ref:ExampleRef = ExampleRef.new()
example.extended_ref_checks(ref)Reacted by William Edwards and fkeyz
Metadata
Metadata
Assignees
Labels
bugThis has been identified as a bug
Activity
asmaloney commentedon Feb 10, 2023
Confirming this happens on macOS as well.
macOS 12.6.3
Godot Engine 4.0 rc1
Stack trace
I worked on debugging it and got as far as looking into the VM.
In this case:
the extension is being called with
calland abase_objof typeGDScriptNativeClass.In this case, however:
the extension is being called with
ptrcallandbase_objof typeExample.So it seems that the VM is choosing the wrong code path when types are present on both vars.
I'm not sure who the VM person is to ping, but hopefully the problem will be clear to them.
asmaloney commentedon Feb 17, 2023
It does feel like the issue is on the godot side since it's calling through two different code paths.
I wonder if @vnen is the right person to give input? (Apologies for the ping if not!)
vnen commentedon Feb 21, 2023
I believe this is a similar issue to the one that was fixed by @RandomShaper in #72654
I guess the fix did not spill over to GDExtension because they are set externally. So to be consistent it probably should be added to the API a way to set whether the return value is a raw object or not (this would be handled by godot-cpp so it would be transparent for the users).
This is not a bug BTW, it is an optimization. When the types are exact GDScript can do a ptrcall directly, which is faster than a regular call (since the latter will use dynamic dispatch). When it can't tell the types in advance it has to fallback to the dynamic dispatch.
RandomShaper commentedon Feb 21, 2023
I'm not sure if I'm understanding if this is about an object being returned of being passed as an argument.