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.
This commit is contained in:
minggo 2019-07-22 09:37:21 +08:00 committed by GitHub
parent 3e6b1ffe92
commit b7a6b77789
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 1 deletions

View File

@ -324,7 +324,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
encodedData = (const char*)(node->FirstChild()->Value()); encodedData = (const char*)(node->FirstChild()->Value());
} }
Data ret = defaultValue; Data ret;
if (encodedData) if (encodedData)
{ {
@ -335,6 +335,10 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
ret.fastSet(decodedData, decodedDataLen); ret.fastSet(decodedData, decodedDataLen);
} }
} }
else
{
ret = defaultValue;
}
if (doc) delete doc; if (doc) delete doc;