Skip to content

Commit b7a6b77

Browse files
authored
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.
1 parent 3e6b1ff commit b7a6b77

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

cocos/base/CCUserDefault.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
324324
encodedData = (const char*)(node->FirstChild()->Value());
325325
}
326326

327-
Data ret = defaultValue;
327+
Data ret;
328328

329329
if (encodedData)
330330
{
@@ -335,6 +335,10 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
335335
ret.fastSet(decodedData, decodedDataLen);
336336
}
337337
}
338+
else
339+
{
340+
ret = defaultValue;
341+
}
338342

339343
if (doc) delete doc;
340344

0 commit comments

Comments
 (0)