Don't throw exceptions from operator new by default#6312
Merged
Conversation
Default mode (no exceptions) will no longer use the stdc++ library new allocator when there is not enough memory. Instead, it will return nullptr. This is the pre-exceptions-available behavior (2.5.0 and earlier). When exceptions are enabled, use the real new and throw exceptions that can be caught at higher levels, or which will crash the app with an uncaught exception if they're not handled. Update to esp8266#6309
d-a-v
reviewed
Jul 19, 2019
d-a-v
left a comment
Collaborator
There was a problem hiding this comment.
This PR is wrong, as was #6309
When exceptions are disabled:
- #6309:
- pros:
- new can return null
- in that case constructor is not executed
- legacy arduino can return null too (but executes constructor in that case)
- cons:
- dirty hack
- require lots of changes, probably in external libraries too
- pros:
- this PR:
- pros:
- new can return null
- legacy arduino can return null too
- cons:
- constructor is always executed (even if
this==nullptr) (like legacy arduino does)
- constructor is always executed (even if
- pros:
- nothing:
- pros:
abort()is called (that's c++ standard with-fno-exceptions)- constructor is not executed when nullptr
- we can remove all check for nullptr when calling
new
- cons:
- not compatible with arduino legacy
but majority doesn't care of new return value being nullptr.
- not compatible with arduino legacy
- pros:
d-a-v
approved these changes
Jul 21, 2019
d-a-v
left a comment
Collaborator
There was a problem hiding this comment.
A failing malloc will return nullptr but constructor will be called. On simple objects this is fine since ram is not modified (no issue). On complex object a segfault(LoadStoreException -> abort) will occur causing an exception.
Never mind, this is both legacy and arduino way (be nice with available ram).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Default mode (no exceptions) will no longer use the stdc++ library new
allocator which throw()s and error when there is not enough memory. Instead, it will return
nullptr. This is the pre-exceptions-available behavior (2.5.0 and
earlier).
When exceptions are enabled, use the real new and throw exceptions that
can be caught at higher levels, or which will crash the app with an
uncaught exception if they're not handled.
Update to #6309