-
Notifications
You must be signed in to change notification settings - Fork 599
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
Sophus ensure calls abort()
#533
Comments
Line 101 in d270df2
In principle, one can enable Hope this can help. |
Thanks, but I think the default shall not be aborting. It should be throwing an exception for example, which would crash anyway if it's not catched. By the way, encountering NaN is hardly a situation for aborting a program, NaNs handling depends a lot on the application. So it boils down to Sophus providing better defaults:
|
@TLescoatTFX Thanks for your suggestions. After checking openCV, I think your suggestion is quite reasonable. Here is what openCV do:
Interestingly, I also noticed that #ifdef CV_STATIC_ANALYSIS
// In practice, some macro are not processed correctly (noreturn is not detected).
// We need to use simplified definition for them.
#define CV_Error(code, msg) do { (void)(code); (void)(msg); abort(); } while (0)
#define CV_Error_(code, args) do { (void)(code); (void)(cv::format args); abort(); } while (0)
#define CV_Assert( expr ) do { if (!(expr)) abort(); } while (0) |
I'm glad you find such suggestion reasonable ! The behavior of OpenCV seems also good, I think they put the |
Well, in c++ libraries can do anything when preconditions are violated. In my opinion, calling abort is much better than UB. |
Hard disagree:
Anyway, that's your library, you do what you want, but fixing that faulty behavior would definitely be an improvement |
Yeah, agree~ "The program has unexpectedly finished." with no reason is pretty hard for the user to debug. |
@TLescoatTFX, thanks for you candid feedback. So let me elaborate. In sophus, NAN's on input variables are undocumented (I have to admit) precondition violations. When I will find some time, I make these preconditions more explicit in the comments.
I know there are different schools of thoughts, but in my workflow (as well as the workflows of the companies I worked at), it is actually the opposite. In this case, I will get an error message with a line number of the root of the error without the need to run it with a debugger. And when running it with the debugger, I still get a full stack-trace with std::abort (but not sure this is specific to my setup somehow...). I also believe that it is the opposite of hiding an error. In most cases, such precondition violations, NANs or not normalized quaternions inputs are often caused by bugs in parsing code or numeric optimization routines. Throw an exception or side-stepping the case (e.g. by re-normalizing the quaternion silently), is actually hiding those non-trivial problems, and failing on a precondition violation makes them explicit, and causing a runtime abort often very close to the actual root cause. (A future API extension - following my opinionated design - could offer various But, I know that there is a point of view where folks do not like std::abort on precondition violations. That's why there is the custom SOPHUS_ENABLE_ENSURE_HANDLER where you can hook in your own error handling (e.g. OpenCV's). Having that said, I have not used SOPHUS_ENABLE_ENSURE_HANDLER and similar macros in a while - since I'm mainly working on sophus2 and sophus-rs. So please, @TLescoatTFX, if you have some PRs to make this easier to plug in your own error handling system, I'm more than happy to accept them (and if CI passes). And please ping me since I'm not that often review the Sophus (1) repo anymore. |
I just tried it out and I'm getting this stack-trace in GDB with the corresponding line number (compiled with RelWithDebInfo):
@TLescoatTFX what is you concrete OS and build config? |
The PR above includes an example of how to use a custom SOPHUS_ENABLE_ENSURE_HANDLER and hence throw an exception instead of calling std::abort: Sophus/examples/CMakeLists.txt Line 16 in ae3a904
|
Describe the bug
In my program, Sophus called
std::abort()
:A library shall never ever call
std::abort()
,std::terminate()
,std::exit()
, ... It does not know the gravity of the error and the recovery policy of the caller, so please do not impose one.To Reproduce
SOPHUS_ENSURE
. This is implemented withSOPHUS_DEDAULT_ENSURE_FAILURE_IMPL
, which in turns callsstd::abort()
.Expected behavior
Default implementation should raise an exception. Or just keep going, but don't kill the program !
Sophus version: commit hash
main-1.x
:Sophus/sophus/common.hpp
Line 127 in d270df2
The text was updated successfully, but these errors were encountered: