From 602d89126fe2ebdb0eb106b58f4f3f2f70b851c3 Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 12 May 2014 23:32:51 +0800 Subject: [PATCH] Value: initialize Value with Type::None when its move and copy contructor is called. Memory leak fix in `operator =(Value&&)` move assignment. --- cocos/base/CCValue.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/cocos/base/CCValue.cpp b/cocos/base/CCValue.cpp index 1b8769ad7a..a8ecc73278 100644 --- a/cocos/base/CCValue.cpp +++ b/cocos/base/CCValue.cpp @@ -126,11 +126,13 @@ Value::Value(ValueMapIntKey&& v) } Value::Value(const Value& other) +: _type(Type::NONE) { *this = other; } Value::Value(Value&& other) +: _type(Type::NONE) { *this = std::move(other); } @@ -192,7 +194,6 @@ Value& Value::operator= (const Value& other) default: break; } - _type = other._type; } return *this; } @@ -201,7 +202,7 @@ Value& Value::operator= (Value&& other) { if (this != &other) { - reset(other._type); + clear(); switch (other._type) { case Type::BYTE: @@ -683,14 +684,6 @@ std::string Value::getDescription() void Value::clear() { - reset(Type::NONE); -} - -void Value::reset(Type type) -{ - if (_type == type) - return; - // Free memory the old value allocated switch (_type) { @@ -724,6 +717,16 @@ void Value::reset(Type type) default: break; } + + _type = Type::NONE; +} + +void Value::reset(Type type) +{ + if (_type == type) + return; + + clear(); // Allocate memory for the new value switch (type)