-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Prevent memory leak when exception is thrown in adl_serializer::to_json #3901
Prevent memory leak when exception is thrown in adl_serializer::to_json #3901
Conversation
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. |
f8cf474
to
db58287
Compare
The test detects the memleak: https://github.com/nlohmann/json/actions/runs/3792554026/jobs/6448842970#step:5:316 |
I went with the solution using a member var, since it requires less changes to the existing code and the resulting code is simpler to understand. |
4018619
to
df39768
Compare
Since this keeps most of the handling in the json class rather than the new data class, such as the copy constructor and move constructor, should the assert invariant function stay there as well? Also, should the copy and move constructors of this class be defaulted? Seems like they should be deleted. |
in that case the assert invariant method is not called if an exception in the ctor occurred. Then we do not need to adapt that method to deal with broken objects. Since the dtor of an element is called after the objects dtor finishes, the call order would be OK as well (assert before destroy).
we need at least the move ctor json/include/nlohmann/json.hpp Line 1196 in df39768
The others we could delete. |
580c36e
to
0e2f01a
Compare
0e2f01a
to
cd0a103
Compare
tests/src/unit-regression2.cpp
Outdated
@@ -36,7 +36,7 @@ using ordered_json = nlohmann::ordered_json; | |||
#include <variant> | |||
#endif | |||
|
|||
#ifdef JSON_HAS_CPP_20 | |||
#ifdef JSON_HAS_CPP_20 && __has_include(<span>) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proper form for this is #if defined(JSON_HAS_CPP_20) && ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I should sleep instead of writing code while tired...
0ed7496
to
e9e1543
Compare
I looked a bit at the ci_test_compilers_clang (10) issue. It seems the compiler and changed to a newer version. old:
new:
Since this issue has nothing to do with this PR. it probably should be fixed in a separate PR. |
…lizer (problem described in issue nlohmann#3881)
* Add checks for uninitialized m_value (this can happen due to exceptions in the ctor) * Add missing noexcept * Fix clang tidy errors
e9e1543
to
1210c3e
Compare
8e99a23
to
79bae8a
Compare
Work on this PR is done and it only needs a review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Thanks! |
(problem described in issue #3881)
There is a memory leak when throwing inside an adl serializers
to_json
method.Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filessingle_include/nlohmann/json.hpp
andsingle_include/nlohmann/json_fwd.hpp
. The whole process is described here.