【发布时间】:2011-07-03 17:42:23
【问题描述】:
我已经阅读了各种关于不应如何在一个 DLL 中分配堆内存并从该 DLL 外部释放它的内容。但是抛出一个只是临时的异常对象(就像大多数异常对象一样)呢?例如:
throw my_exception( args ); // temporary: no heap allocation
当异常对象在 DLL 之外被捕获时,该对象的析构函数最终将被执行,并且该对象的非堆内存将被回收。因为它不是堆内存,所以可以吗?
【问题讨论】:
-
你抛出的异常不是你捕获的异常;沿途制作了一份副本。也就是说,我不知道副本在哪里,也不知道它是如何被破坏的。
-
@Mark:当异常被引用捕获时,这仍然是真的吗?你有消息来源吗?
-
@Mark:AFAIK,C++ 要求异常具有可公开访问的复制构造函数,但是否实际使用它来制作副本取决于实现。它很可能是被捕获的同一物体。
-
@Ben:来自 N3225,
15.1 Throwing an exception,3: A throw-expression initializes a temporary object...,5: The memory for the exception object is allocated in an unspecified way...@Paul:5: When the thrown object is a class object, the copy/move constructor and the destructor shall be accessible, even if the copy/move operation is elided -
@Ben:来自 N3225,
15.3 Handling an exception16: The object declared in an exception-declaration or, if the exception-declaration does not specify a name, a temporary (12.2) is copy-initialized (8.5) from the exception object...17: ...When the handler declares a reference to a non-constant object, any changes to the referenced object are changes to the temporary object initialized when the throw-expression was executed and will have effect should that object be rethrown.
标签: c++ dll exception-handling