Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
问题描述
我已经阅读了关于不应如何在一个 DLL 中分配堆内存并从该 DLL 外部释放它的各种内容.但是如果抛出一个只是临时的异常对象(大多数异常对象都是如此)呢?例如:
I've read various things about how one should not allocate heap memory in one DLL and deallocate it from outside that DLL. But what about throwing an exception object that is a just a temporary (as most exception objects are)? E.g.:
throw my_exception( args ); // temporary: no heap allocation
当异常对象在 DLL 外被捕获时,最终会执行该对象的析构函数,并回收该对象的非堆内存.因为它不是堆内存所以可以吗?
When the exception object is caught outside the DLL, the destructor for that object will eventually be executed and the non-heap memory for the object will be reclaimed. Is that OK since it's not heap memory?
推荐答案
只有当所有模块都使用相同的 C++ 运行时(在这种情况下它们也共享一个堆)时,才可能跨 DLL 边界抛出 C++ 异常.但这可能是一种维护负担,尤其是当涉及多个供应商的库时,因此不鼓励这样做.
Throwing C++ exceptions across DLL boundaries is only possible when all modules use the same C++ runtime, in which case they share a heap as well. But this can be a maintenance burden, especially when libraries from multiple vendors are involved, so it is discouraged.
如果您想要跨多个编译器/编译器版本/编译器设置进行可移植的错误处理,请使用返回代码或操作系统提供的异常(例如 Windows 上的 SEH)/
If you want error-handling which is portable across multiple compilers/compiler versions/compiler settings, either use return codes or OS-provided exceptions (e.g. SEH on Windows)/
这篇关于跨 DLL 边界抛出 C++ 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:跨 DLL 边界抛出 C++ 异常
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
