Skip to content

Commit e8bc4b3

Browse files
JohnCoconutminggo
authored andcommitted
fix memory leak in CCUserDefault (#19853)
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 4afda3a commit e8bc4b3

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

cocos/base/CCUserDefault.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
321321
encodedData = (const char*)(node->FirstChild()->Value());
322322
}
323323

324-
Data ret = defaultValue;
324+
Data ret;
325325

326326
if (encodedData)
327327
{
@@ -332,6 +332,10 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
332332
ret.fastSet(decodedData, decodedDataLen);
333333
}
334334
}
335+
else
336+
{
337+
ret = defaultValue;
338+
}
335339

336340
delete doc;
337341

0 commit comments

Comments
 (0)