Storing and Rethrowing Exceptions

Using std::exception_ptr Across DLL Boundaries

Can I use std::exception_ptr to transfer exceptions across DLL boundaries?

Abstract art representing computer programming

Yes, std::exception_ptr can be used to transfer exceptions across DLL (dynamic-link library) boundaries, but there are some important considerations.

When an exception is thrown in a DLL and caught in the main executable (or vice versa), the exception object must be properly destructed. If the DLL and the executable use different C++ runtimes (which is common), this can lead to runtime errors or even crashes, because the exception object is not destructed properly.

std::exception_ptr helps mitigate this issue by providing a way to store and transfer the exception without depending on the specific exception object. The DLL can catch the exception, store it in a std::exception_ptr, and then transfer this std::exception_ptr to the executable. The executable can then rethrow the exception using std::rethrow_exception().

Here's a simplified example:

// In the DLL
#include <exception>
#include <stdexcept>

extern "C" __declspec(dllexport)
std::exception_ptr captureException() {
  try {
    throw std::runtime_error("Exception from DLL");
  } catch (...) {
    return std::current_exception();
  }
}

// In the executable
#include <exception>
#include <iostream>
#include <stdexcept>

// function from DLL
std::exception_ptr captureException();

int main() {
  std::exception_ptr eptr = captureException();
  try {
    if (eptr) {
      std::rethrow_exception(eptr);
    }
  } catch (const std::exception& e) {
    std::cout << "Caught exception from DLL: "
      << e.what() << "\n";
  }
}
Caught exception from DLL: Exception from DLL

In this example, the exception is thrown and captured into a std::exception_ptr inside the DLL. This std::exception_ptr is then returned to the executable, where it's rethrown and caught.

However, it's important to note that while std::exception_ptr allows transferring exceptions across DLL boundaries, it doesn't completely solve all potential issues. For example, if the exception object contains data that is allocated in the DLL, there can still be issues with properly freeing that memory.

Therefore, when using exceptions across DLL boundaries, it's often best to define and use simple, lightweight exception classes that don't allocate resources or contain complex data structures.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved