-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Fix ~basic_json causing std::terminate #4654
base: develop
Are you sure you want to change the base?
Conversation
683c809
to
ee5b79c
Compare
b4da845
to
5afd7be
Compare
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @Ignition |
8bf4770
to
cd04272
Compare
@nlohmann I'm having trouble making the Windows build correct. I don't have windows machine available to debug/diagnose. |
include/nlohmann/json.hpp
Outdated
std::move(array->begin(), array->end(), std::back_inserter(stack)); | ||
} | ||
else | ||
#ifdef __cpp_exceptions |
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.
For what it's worth, this is how we detect exceptions at other parts in the code:
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND))
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.
I'll change that and push up the change
I don't use Windows myself. :-/ |
If there is an allocation problem in `destroy` fallback to recursion approach. Signed-off-by: Gareth Lloyd <[email protected]>
I'm not wild about this idea. It's swallowing unknown exceptions and setting up a stack overflow. |
I am not sure what to make of this right now. The noexcept annotation was from times where we did hardly anything in the destructor. |
Destructors are |
Normally I would fully agree about swallowing unknown exceptions being bad. But in this case with the current implementation there are only a few potential sources of exceptions.
So it just a question about is it safe to swallow unknown exception from the allocation subsystem. My feeling is yes, because its actually not swallowing, but handling a corner case even if you don't know what that exception is. You either:
|
Do you use the I think it's possible to rewrite this to not use the vector if the I haven't written any of this code, just thinking about what might be possible. |
@gregmarr @nlohmann @gregmarr I agree it is possible to do it without heap allocation. I think that modification is worth doing (assuming benchmarks show that is fine). Here is Herb Sutter talking about destroying trees https://youtu.be/JfmTagWcqoE?si=XyJR0YpYZGm4FGbB&t=970 |
@Ignition Yeah, the current method is the bottom row of the table, O(N) heap space with moves. In order to remove the O(N) heap space, which is the issue that you're having of allocating memory in a destructor, we'd have to go to the second row of the table, which is O(N log N) time. |
During
~basic_json
, it was possible to receive an exception from the allocation subsystem (quota/policy violation etc). This would causestd::terminate
because destructors are noexcept.This fix will gracefully handle that case by allowing RAII to handle anything within the current stack (used to avoid recursion), then fallback to the recursion approach for anything that still remains.
fixes #4653