Skip to content

memory leak with Data::fastSet #19852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JohnCoconut opened this issue Jun 21, 2019 · 0 comments
Closed

memory leak with Data::fastSet #19852

JohnCoconut opened this issue Jun 21, 2019 · 0 comments

Comments

@JohnCoconut
Copy link
Contributor

cocos2d-x version: current v3 branch

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,

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant