diff options
author | Ruslan Mustakov <r.mustakov@gmail.com> | 2017-08-19 19:41:11 +0700 |
---|---|---|
committer | Ruslan Mustakov <r.mustakov@gmail.com> | 2017-09-14 19:40:36 +0700 |
commit | f08bc0df7c16a6d12292628ec8cc2e015047c450 (patch) | |
tree | 73b9bdaf5441798340bcbddcd96edc2430a9ccd0 /modules | |
parent | 9ac940677c9febc5f1c52782a717df61b0224344 (diff) |
Construct Variants from Reference properly in GDNative
Previously godot_variant_new_object constructed Variant without
accounting for the fact that the Object can be a Reference, so refcount
was not increased and References were destructed prematurely.
Also, Reference::init_ref did not propagate refcount increment to the
script instance, which led to desync of refcount info on the script
side and Godot side.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gdnative/gdnative/variant.cpp | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/modules/gdnative/gdnative/variant.cpp b/modules/gdnative/gdnative/variant.cpp index b61f80b1f9..1b2aae607f 100644 --- a/modules/gdnative/gdnative/variant.cpp +++ b/modules/gdnative/gdnative/variant.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "gdnative/variant.h" +#include "core/reference.h" #include "core/variant.h" #ifdef __cplusplus @@ -158,7 +159,21 @@ void GDAPI godot_variant_new_rid(godot_variant *r_dest, const godot_rid *p_rid) void GDAPI godot_variant_new_object(godot_variant *r_dest, const godot_object *p_obj) { Variant *dest = (Variant *)r_dest; Object *obj = (Object *)p_obj; - memnew_placement_custom(dest, Variant, Variant(obj)); + Reference *reference = Object::cast_to<Reference>(obj); + REF ref; + if (reference) { + ref = REF(reference); + } + if (!ref.is_null()) { + memnew_placement_custom(dest, Variant, Variant(ref.get_ref_ptr())); + } else { +#if defined(DEBUG_METHODS_ENABLED) + if (reference) { + ERR_PRINT("Reference object has 0 refcount in godot_variant_new_object - you lost it somewhere."); + } +#endif + memnew_placement_custom(dest, Variant, Variant(obj)); + } } void GDAPI godot_variant_new_dictionary(godot_variant *r_dest, const godot_dictionary *p_dict) { |