跨DLL边界抛出C ++异常 [英] Throwing C++ exceptions across DLL boundaries

查看:183
本文介绍了跨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 ++异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆