-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Closed
Description
cocos2d-x version: current v3 branch
cocos2d-x/cocos/base/CCData.cpp
Lines 127 to 133 in 4afda3a
void Data::fastSet(unsigned char* bytes, const ssize_t size) | |
{ | |
CCASSERT(size >= 0, "fastSet size should be non-negative"); | |
//CCASSERT(bytes, "bytes should not be nullptr"); | |
_bytes = bytes; | |
_size = size; | |
} |
fastSet
makes the Data
object managing a new memory area in [bytes, bytes + size)
, but it doesn't releasing the old data it manages. Failing to release the old data causes memory leak, as happens below,
cocos2d-x/cocos/base/CCUserDefault.cpp
Lines 324 to 334 in 4afda3a
Data ret = defaultValue; | |
if (encodedData) | |
{ | |
unsigned char * decodedData = nullptr; | |
int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData); | |
if (decodedData) { | |
ret.fastSet(decodedData, decodedDataLen); | |
} | |
} |
Can be tested with the following example using valgrind
int main(int argc, char **argv)
{
UserDefault *userDefault = UserDefault::getInstance();
Data defaultValue;
unsigned char *someData = (unsigned char*)malloc(100);
defaultValue.fastSet(someData, 100);
Data newData = userDefault->getDataForKey("int_data", defaultValue);
}
with the following UserDefault.xml
file,
<?xml version="1.0" encoding="UTF-8"?>
<userDefaultRoot>
<int_data>BQAAAAQAAAADAAAAAgAAAAEAAAAAAAAA</int_data>
<float_data>AACgQAAAgEAAAEBAAAAAQAAAgD8AAAAA</float_data>
<double_data>AAAAAAAAFEAAAAAAAAAQQAAAAAAAAAhAAAAAAAAAAEAAAAAAAADwPwAAAAAAAAAA</double_data>
</userDefaultRoot>
Metadata
Metadata
Assignees
Labels
No labels