-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
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
[GDNative] Crash when method returns reference objects #35609
Comments
The godot-cpp |
@karroffel I'm really not sure how godot-cpp can handle this correctly The generated code for Reference *Object::get_script() const {
return (Reference *) ___godot_icall_Object(___mb.mb_get_script, (const Object *) this);
}
static inline Object *___godot_icall_Object(godot_method_bind *mb, const Object *inst) {
godot_object *ret;
ret = nullptr;
const void *args[1] = {
};
godot::api->godot_method_bind_ptrcall(mb, inst->_owner, args, &ret);
if (ret) {
return (Object *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, ret);
}
return (Object *) ret;
} so it's pretty similar to what I'm doing: def get_script(self,):
cdef godot_object *__ret
[...]
gdapi10.godot_method_bind_ptrcall(
__methbind__Object__get_script,
self._gd_ptr,
NULL,
&__ret
)
[...] However this end up with the following stack
Note the |
I think I finally got the issue: After that, Finally this code got called: Line 277 in b0eca58
So the pointer is used if it is not null, hence causing the segfault :/ Fortunately, PR #35813 should fix this error prone behavior for Godot 4.0 ;-) |
Unlike other Godot objects, Reference objects (like all the
Resource
for instance) are refcounted.Hence they are not passed as a simple
Object *
but as aRefPtr
.This is a trouble for GDNative given it doesn't provide any binding to this
RefPtr
structure (nor any information about how to handle this).For instance the
RefPtr Object::get_script() const
is defined inapi.json
as:From those information you would expect to write like this:
However this cause segfault when
godot_method_bind_ptrcall
tries to store the returnedRefPtr
into thegodot_object *ret
:'(BTW, there is a
is_reference
flag in the class definition, butReference
is not marked asis_reference
(only it children are), though I'm not sure what's the reason, I would guess it's an implementation error from:godot/modules/gdnative/nativescript/api_generator.cpp
Lines 221 to 224 in 9daaa12
To solve this issue, Godot-CPP provide it own custom Ref class:
https://github.com/GodotNativeTools/godot-cpp/blob/81783c6045ac5e3638846f791c7c7841db38aa7e/include/core/Ref.hpp
This solution works because Godot-CPP is in C++ and can then reproduce the layout of the internal Godot RefPtr (though I'm not really sure this is 100% safe if Godot is compiled with a different compiler than the GDNative plugin using Godot-CPP... @karroffel can you enlighten me on this ? ^^ )
Anyway, I guess we should provide a binding to the
RefPtr
in GDNative and fix theis_reference
flag (maybe even provide thisis_reference
in the method's flags ?), what do you think ?The text was updated successfully, but these errors were encountered: