如何确定 Delphi 的 THeapException 是否与堆相关,而不是 Windows GDI 或其他?

How to decide if Delphi's THeapException is heap related, not Windows GDI or something else?

在Delphi的VCL class库中,EOutOfResources异常class定义为EOutOfMemory的subclass,即subclass 的 EHeapException,它是基础异常 class 的子 class,异常。 http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.Classes.EOutOfResources

EOutOfResources 的描述是:"EOutOfResources is raised when an application attempts to create a Windows or widget handle and there are no more handles to allocate."

EOutOfMemory 的描述说:"EOutOfMemory occurs when an application attempts to allocate dynamic memory, but there is not enough free memory in the system to meet the request."

对我来说,堆内存是 "resource" 的一个特例,Windows GDI 句柄与堆内存关系很小或根本没有关系。这基本上是一个错误和建模错误吗?或者有什么方法可以可靠地知道异常是否 实际上 堆相关?为每种异常类型编写特殊情况处理代码,并忽略 class 层次结构?

在异常处理程序中处理异常对象时,即使它"is-a EHeapException"在语言句法意义上,它也可能根本不是堆相关的异常,采取的纠正措施应该是完全不同的.看起来,我什至不能使用 "is" 语句来处理 VCL 引发的错误,更不用说第三方组件了,它们在建模错误情况和异常时似乎更轻松。

层次结构是这样的:

EHeapException
|
|-- EInvalidPointer
|
|-- EOutOfMemory
    |
    |-- EOutOfResources

所以你可以这样测试:

if E is EOutOfResources then
  // probably GDI resource leak
else if E is EOutOfMemory then
  // probably memory or address space is exhausted 
else if E is EInvalidPointer then
  // probably heap corruption