-
Notifications
You must be signed in to change notification settings - Fork 46
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
Should cpp11::stop just throw an exception rather than using unwind_protect? #219
Comments
I think the motivation for this was probably readxl: I am revisiting this code, to introduce similar error code checks in another constructor, and have re-established that any natural call to It reliably leads to:
I'm just noting this in case anyone ever works on this and needs an (non)working example of the problem. I can provide! |
If you have such an example, I'll take it :-) |
You can experience the problem by translating this error-throwing code to a simple call to Here's a PR where I've induced the problem: tidyverse/readxl#690 I get a segfault ( Note that there are several other successful uses of This commit points you at some interesting places to experiment with |
This is an example created for r-lib/cpp11#219.
Interestingly, my PR appears to only segfault on macOS. I see it locally and that is the only CI job that fails. |
Just a quick +1 for "throwing an exception instead"...in developing user-defined functions (and other "calling into R from other threads" stuff) in Arrow, I ended up with a few cases where the |
Emulating the printf-like behaviour isn't too bad...this bit of code is mostly untested and was from an early prototype of geoarrow, but might help? class Exception: public std::exception {
public:
Exception() {
memset(error_, 0, sizeof(error_));
}
Exception(const std::string& error) {
memset(error_, 0, sizeof(error_));
if (error.size() >= (8096 - 1)) {
memcpy(error_, error.data(), 8096 - 1);
} else if (error.size() > 0) {
memcpy(error_, error.data(), error.size());
}
}
Exception(const char* fmt, ...) {
memset(error_, 0, sizeof(error_));
va_list args;
va_start(args, fmt);
vsnprintf(error_, sizeof(error_) - 1, fmt, args);
va_end(args);
}
const char* what() const noexcept {
return error_;
}
protected:
char error_[8096];
}; |
It seems there are some cases where use of unwind_protect causes issues, since there is a long jump involved, such as within constructors.
It might be better to have it simply throw an exception instead.
The text was updated successfully, but these errors were encountered: