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

Pass MSVC exception through SetUnhandledExceptionFilter #336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nanoant
Copy link

@nanoant nanoant commented Apr 26, 2024

This restores ability to chain std::set_terminate handlers on Windows by passing MSVC C++ runtime exceptions using their 0xE06D7363 system code back to original handler that will later call backward-cpp's terminator.

Without this other std::set_terminate handlers were blocked when using backward-cpp.

Accompanying example below. Running test.exe ex shows that custom termination handler is now effective and rethrowing prints proper stack trace.

#include <cstring>
#include <stdexcept>
#include "backward.hpp"

backward::SignalHandling sh;

void crash(const char* mode) {
  if (!std::strcmp(mode, "ex")) {
    throw std::runtime_error("std exception test");
  }
  if (!std::strcmp(mode, "strex")) {
    throw "string exception test";
  }
  int* n = nullptr;
  *n = 1;
}

void pass2(const char* mode) { crash(mode); }

void pass1(const char* mode) { pass2(mode); }

int main(int argc, char const *argv[]) {
  std::set_terminate([]() {
    const std::exception_ptr ex = std::current_exception();
    try {
        std::rethrow_exception(ex);
    } catch (std::exception ex) {
        std::cerr << "Uncaught exception: " << ex.what() << std::endl;
    }
  });
  const char* mode = argc == 2 ? argv[1] : "crash";
  std::cerr << "mode: " << mode << std::endl;
  pass1(mode);
}

This restores ability to chain std::set_terminate handlers on Windows by
passing MSVC C++ runtime exceptions using their 0xE06D7363 system code
back to original handler that will later call backward-cpp's terminator.

Without this other std::set_terminate handlers were blocked when using
backward-cpp.
@mgerhardy
Copy link
Contributor

mgerhardy added a commit to vengi-voxel/vengi that referenced this pull request Sep 16, 2024
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