diff options
| author | karroffel <therzog@mail.de> | 2018-01-19 11:40:50 +0100 |
|---|---|---|
| committer | karroffel <therzog@mail.de> | 2018-01-19 11:40:50 +0100 |
| commit | 2e4de7b67dcea307cbb306739d71a6d983165684 (patch) | |
| tree | e9fcd41d5e70dd30438af9f1477fe3f4b9dedd21 /include/core | |
| parent | 131cf2581e05897ecb40ad527a01cd9068c58628 (diff) | |
| download | redot-cpp-2e4de7b67dcea307cbb306739d71a6d983165684.tar.gz | |
Don't leak memory from Ref<>()
Diffstat (limited to 'include/core')
| -rw-r--r-- | include/core/Ref.hpp | 93 |
1 files changed, 36 insertions, 57 deletions
diff --git a/include/core/Ref.hpp b/include/core/Ref.hpp index 345b455..043db57 100644 --- a/include/core/Ref.hpp +++ b/include/core/Ref.hpp @@ -10,24 +10,6 @@ template<class T> class Ref { T *reference; - void ref(const Ref &from) - { - if (from.reference == reference) return; - - unref(); - - reference = from.reference; - if (reference) reference->reference(); - } - - void ref_pointer(T *r) - { - if (!r) return; - - if (r->init_ref()) reference = r; - } - - public: inline bool operator==(const Ref<T> &r) const { @@ -73,28 +55,37 @@ public: void operator=(const Ref &from) { - ref(from); + if (reference) + unref(); + + if (from.reference) { + reference = from.reference; + reference->reference(); + } else { + reference = nullptr; + } } template<class T_Other> void operator=(const Ref<T_Other> &from) { - Ref<T> n((T *) from.ptr()); - ref(n); + if (reference) + unref(); + + if (from.reference) { + reference = (T *) from.reference; + reference->reference(); + } else { + reference = nullptr; + } } void operator=(const Variant &variant) { - T *r = (T *) (Object *) variant; - if (!r) { + if (reference) unref(); - return; - } - - Ref re; - re.reference = r; - ref(re); - re.reference = nullptr; + + reference = (T *) (Object *) variant; } operator Variant() const @@ -105,22 +96,27 @@ public: template<class T_Other> Ref(const Ref<T_Other> &from) { - if (from.ptr()) - ref_pointer((T *) from.ptr()); - else + if (from.reference) { + reference = (T *) from.reference; + reference->reference(); + } else { reference = nullptr; + } } Ref(const Ref &from) { - reference = nullptr; - ref(from); + if (from.reference) { + reference = from.reference; + reference->reference(); + } else { + reference = nullptr; + } } Ref(T *r) { - r->reference(); reference = r; } @@ -129,28 +125,9 @@ public: Ref(const Variant &variant) { - reference = nullptr; - T *r = (T *) (Object *) variant; - if (!r) { - unref(); - return; - } - - Ref re; - re.reference = r; - ref(re); - re.reference = nullptr; + reference = (T *) (Object *) variant; } - template<class T_Other> - static Ref<T> __internal_constructor(T_Other *r) - { - Ref<T> ref; - ref.reference = (T *) r; - return ref; - } - - inline bool is_valid() const { return reference != nullptr; } inline bool is_null() const { return reference == nullptr; } @@ -164,7 +141,9 @@ public: void instance() { - ref(new T); + unref(); + + reference = new T; } Ref() |
