Skip to content
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

Don't throw exceptions from operator new by default #6312

Merged
merged 3 commits into from
Jul 23, 2019

Conversation

earlephilhower
Copy link
Collaborator

@earlephilhower earlephilhower commented Jul 18, 2019

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

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
@earlephilhower earlephilhower requested a review from d-a-v July 18, 2019 15:18
Copy link
Collaborator

@d-a-v d-a-v left a comment

Choose a reason for hiding this comment

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

This PR is wrong, as was #6309
When exceptions are disabled:

  • operator new: no exception without need for std::nothrow #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
  • 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)
  • 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.

Copy link
Collaborator

@d-a-v d-a-v left a comment

Choose a reason for hiding this comment

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

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).

@d-a-v d-a-v reopened this Jul 23, 2019
@d-a-v d-a-v merged commit 82a1382 into esp8266:master Jul 23, 2019
@earlephilhower earlephilhower deleted the nonewsgnus branch July 25, 2019 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants