Skip to content

Commit feaba55

Browse files
authored
Merge pull request #1045 from zhehangd/fix_ref_crash
Fix crash using Ref<T> as parameter
2 parents e9942db + 517db66 commit feaba55

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

include/godot_cpp/classes/ref.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ class Ref {
240240
template <class T>
241241
struct PtrToArg<Ref<T>> {
242242
_FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
243-
GDExtensionRefPtr ref = (GDExtensionRefPtr)p_ptr;
244-
ERR_FAIL_NULL_V(ref, Ref<T>());
245-
246-
T *obj = reinterpret_cast<T *>(godot::internal::gde_interface->object_get_instance_binding(godot::internal::gde_interface->ref_get_object(ref), godot::internal::token, &T::___binding_callbacks));
247-
return Ref<T>(obj);
243+
// Important: p_ptr is T*, not Ref<T>*, since Object* is what engine gives to ptrcall.
244+
ERR_FAIL_NULL_V(p_ptr, Ref<T>());
245+
return Ref<T>(reinterpret_cast<T *>(godot::internal::gde_interface->object_get_instance_binding(
246+
reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)),
247+
godot::internal::token, &T::___binding_callbacks)));
248248
}
249249

250250
typedef Ref<T> EncodeT;
@@ -266,7 +266,10 @@ struct PtrToArg<const Ref<T> &> {
266266
typedef Ref<T> EncodeT;
267267

268268
_FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
269-
return Ref<T>(reinterpret_cast<T *>(godot::internal::gde_interface->object_get_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr)), godot::internal::token, &T::___binding_callbacks)));
269+
ERR_FAIL_NULL_V(p_ptr, Ref<T>());
270+
return Ref<T>(reinterpret_cast<T *>(godot::internal::gde_interface->object_get_instance_binding(
271+
reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)),
272+
godot::internal::token, &T::___binding_callbacks)));
270273
}
271274
};
272275

0 commit comments

Comments
 (0)