jsoncpp icon indicating copy to clipboard operation
jsoncpp copied to clipboard

Change signature of newCharReader to return uniqu_ptr

Open AssafPassalCG opened this issue 3 years ago • 5 comments

Description The current implementation of Json::CharReaderBuilder.newCharReader(), allocates memory, while there is no one to release it, this issue causes a leak because when we upgraded to the new version, we didn't understand we should wrap the pointer with std::unique_ptr

Solution Make the function return std::unique_ptr

Context I opened that PR: https://github.com/open-source-parsers/jsoncpp/pull/1420, and that closed because it is a breaking change. While I see the point of that, I actually think that if it will break someone's code, there is a good chance that it will save him from a memory leak.

AssafPassalCG avatar Jul 23 '22 21:07 AssafPassalCG

We can't change the signature of this old and fundamental function.

We have a lot of user code written for that raw pointer return type, and changing it would require a change to nearly all programs that use the jsoncpp library. It's not IMO a problem requiring such a drastic change. C++ programmers are assumed to take care with raw pointers and avoid memory leaks, and ideally install leak detectors on their CI builds.

What we should do, however, is provide a similar but different function that returns a std::unique_ptr<CharReader>, and encourage transition to that one. Maybe makeCharReader? newCharReader can be discouraged and deprecated down the road but we do not need to remove it.

BillyDonahue avatar Jul 23 '22 22:07 BillyDonahue

I will create the PR, soon I hope

AssafPassalCG avatar Jul 26 '22 14:07 AssafPassalCG

Not sure how to mark the deprecated function

AssafPassalCG avatar Jul 26 '22 14:07 AssafPassalCG

https://github.com/open-source-parsers/jsoncpp/pull/1424

AssafPassalCG avatar Jul 26 '22 14:07 AssafPassalCG

I finished the PR changes you requested, and most of the changes happened because I used reformat.sh script from the repo, maybe it a good idea to put some note that you shouldn't commit those changes

AssafPassalCG avatar Aug 05 '22 12:08 AssafPassalCG