-
Notifications
You must be signed in to change notification settings - Fork 13.3k
operator new: no exception without need for std::nothrow #6309
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
Conversation
…of throwing an exception without (std::nothrow)
Interesting solution to the problem. The standard libraries do not ever check the return value of new() != nullptr, they use a Theoretically, it seems like this would let the std:: libs abort() on the failed new (since they should use the base new() and it will throw), and let user code use the broken nothrow new(). |
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.
Needs to set the last alloc addr/size on failure to help us debug crashes.
I'll try it out later today on some test cases, too, with and w/o exceptions...
cores/esp8266/Arduino.h
Outdated
#include <bits/c++config.h> | ||
extern "C++" inline void* operator new (std::size_t size) noexcept | ||
{ | ||
return malloc(size); |
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.
Please check out the old new() and new override, it sets the last-failed-alloc address which this needs to do as well:
Lines 31 to 49 in bb28d4a
void *operator new(size_t size) | |
{ | |
void *ret = malloc(size); | |
if (0 != size && 0 == ret) { | |
umm_last_fail_alloc_addr = __builtin_return_address(0); | |
umm_last_fail_alloc_size = size; | |
} | |
return ret; | |
} | |
void *operator new[](size_t size) | |
{ | |
void *ret = malloc(size); | |
if (0 != size && 0 == ret) { | |
umm_last_fail_alloc_addr = __builtin_return_address(0); | |
umm_last_fail_alloc_size = size; | |
} | |
return ret; | |
} |
This is a hack. Remove |
For reference the prior new override was handled here: |
(this is going to be a noisy PR)
(for devs with constraints who know what they are doing)
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
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
CLOSED
https://stackoverflow.com/questions/6049563/with-fno-exceptions-what-happens-with-new-t
operator new
will never return nullptr.Either we get an exception, or if everything is compiled with
-fno-exception
,abort()
is called.(was:)
Overloads
operator new
so it can returnnullptr
instead of throwing an exception without need for(std::nothrow)
.Requirement:
<Arduino.h>
must be included prior to any c++ header fileaddresses #6269 @mcspr @mhightower83