From b7a6b7778918f0f530b4f9167ea1bbd11aafb6af Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 22 Jul 2019 09:37:21 +0800 Subject: [PATCH] fix memory leak in CCUserDefault (#19853) (#19947) fastSet makes the Data object managing a new memory area in [bytes, bytes + size), but it doesn't releasing the old data it managed. Failure to release the old data causes memory leak. The default constructed Data manages null memory, so calling fastSet on it is fine. Because `Data ret = defaultValue;` malloc new memory, we might have better performance without it. --- cocos/base/CCUserDefault.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cocos/base/CCUserDefault.cpp b/cocos/base/CCUserDefault.cpp index cb3f713942..6b4da15a60 100644 --- a/cocos/base/CCUserDefault.cpp +++ b/cocos/base/CCUserDefault.cpp @@ -324,7 +324,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) encodedData = (const char*)(node->FirstChild()->Value()); } - Data ret = defaultValue; + Data ret; if (encodedData) { @@ -335,6 +335,10 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) ret.fastSet(decodedData, decodedDataLen); } } + else + { + ret = defaultValue; + } if (doc) delete doc;