From 66c671b59dece97d523478cae7c602de6d423aa4 Mon Sep 17 00:00:00 2001 From: sheepandshepherd Date: Wed, 4 Dec 2019 21:12:59 +0100 Subject: [PATCH] Add C conversion constructors to fix leak of default-constructed empty arrays --- include/core/Array.hpp | 5 +++++ include/core/Dictionary.hpp | 5 +++++ src/core/Variant.cpp | 6 ++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/core/Array.hpp b/include/core/Array.hpp index d766c549..3d13914c 100644 --- a/include/core/Array.hpp +++ b/include/core/Array.hpp @@ -57,6 +57,11 @@ class Object; class Array { godot_array _godot_array; + friend class Variant; + inline explicit Array(const godot_array &other) { + _godot_array = other; + } + public: Array(); Array(const Array &other); diff --git a/include/core/Dictionary.hpp b/include/core/Dictionary.hpp index ec976020..7f40adc9 100644 --- a/include/core/Dictionary.hpp +++ b/include/core/Dictionary.hpp @@ -12,6 +12,11 @@ namespace godot { class Dictionary { godot_dictionary _godot_dictionary; + friend Variant::operator Dictionary() const; + inline explicit Dictionary(const godot_dictionary &other) { + _godot_dictionary = other; + } + public: Dictionary(); Dictionary(const Dictionary &other); diff --git a/src/core/Variant.cpp b/src/core/Variant.cpp index 4c5cfe66..86d2f911 100644 --- a/src/core/Variant.cpp +++ b/src/core/Variant.cpp @@ -261,14 +261,12 @@ Variant::operator RID() const { } Variant::operator Dictionary() const { - Dictionary ret; - *(godot_dictionary *)&ret = godot::api->godot_variant_as_dictionary(&_godot_variant); + Dictionary ret(godot::api->godot_variant_as_dictionary(&_godot_variant)); return ret; } Variant::operator Array() const { - Array ret; - *(godot_array *)&ret = godot::api->godot_variant_as_array(&_godot_variant); + Array ret(godot::api->godot_variant_as_array(&_godot_variant)); return ret; }