Why not raise EInvalidPointer?
Delphi文档说明:
Never raise an EInvalidPointer exception directly. EInvalidPointer is raised internally by the memory manager.
我正在编写一个自定义基类作为
1 2 3 4 5 | procedure TInterfacedObject.BeforeDestruction; begin if RefCount <> 0 then Error(reInvalidPtr); end; |
其中
如果我正在编写自己的类,我该如何实现
1 2 3 4 5 | procedure TMyInterfacedObject.BeforeDestruction; begin if RefCount <> 0 then raise EInvalidPointer.CreateRes(@SInvalidPointer) at ReturnAddress; end; |
在
大卫回答的补充; 有关
EHeapException is the exception class for errors related to heap-allocated memory.
EHeapException's descendants—EOutOfMemory and EInvalidPointer—are used
to handle failed allocations of dynamic memory and invalid pointer
operations.Note: Memory for these exceptions is pre-allocated whenever an application starts and remains allocated as long as the application is
running. Never raise EHeapException or its descendants directly.
我猜测的是,一旦你遇到内存问题,分配内存来创建这些错误可能不安全:因为缺乏内存或可能存在腐败......
避开原始问题,您可以避免仅使用与运行时相同的代码来询问它:
1 | System.Error(reInvalidPtr); |