From f2611c1b63abf5eb66d39bba1739821e12c0bfc8 Mon Sep 17 00:00:00 2001 From: John Zhang Date: Sat, 22 Jun 2019 03:35:57 +0800 Subject: [PATCH] fix memory leak in CCUserDefault 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 b1cc83235bb7..ff88a3ec29df 100644 --- a/cocos/base/CCUserDefault.cpp +++ b/cocos/base/CCUserDefault.cpp @@ -321,7 +321,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) encodedData = (const char*)(node->FirstChild()->Value()); } - Data ret = defaultValue; + Data ret; if (encodedData) { @@ -332,6 +332,10 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) ret.fastSet(decodedData, decodedDataLen); } } + else + { + ret = defaultValue; + } delete doc;