Skip to content

fix memory leak in CCUserDefault #19853

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

Merged
merged 1 commit into from
Jun 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cocos/base/CCUserDefault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
encodedData = (const char*)(node->FirstChild()->Value());
}

Data ret = defaultValue;
Data ret;

if (encodedData)
{
Expand All @@ -332,6 +332,10 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
ret.fastSet(decodedData, decodedDataLen);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check other places where fastSet is used?
I think that this method should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CCData has an unintuitive interface and incomplete documentation. I hope it behaves like smart pointers, since it manages memory ownership.

fastSet could be like unique_ptr::reset(p) by freeing previously managed memory, and obtaining ownership of p.

copy method has somewhat misleading documentation.

/** Copies the buffer pointer and its size.
* @note This method will copy the whole buffer.
* Developer should free the pointer after invoking this method.
* @see Data::fastSet
* @return The size of bytes copied, return 0 if size <= 0
*/
ssize_t copy(const unsigned char* bytes, const ssize_t size);

Developer should free the pointer after invoking this method.

Which pointer does it refers to in free the pointer? If it means the buffer pointer, we could call reset instead?

}
}
else
{
ret = defaultValue;
}

delete doc;

Expand Down