为什么不提高EInvalidPointer? [英] Why not raise EInvalidPointer?

查看:133
本文介绍了为什么不提高EInvalidPointer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi 文档状态


切勿直接引发 EInvalidPointer 异常。 EInvalidPointer 由内存管理器在内部引发。

Never raise an EInvalidPointer exception directly. EInvalidPointer is raised internally by the memory manager.

我正在编写自定义基类,以替代 TInterfacedObject ,尽可能紧密地遵循RTL的实现,例如,可以看到RTL中的 TInterfacedObject BeforeDestruction 实现为:

I'm writing a custom base class as an alternative to TInterfacedObject, following the RTL implementation as closely as possible, and see, by example, that TInterfacedObject in the RTL implements BeforeDestruction as:

procedure TInterfacedObject.BeforeDestruction;
begin
  if RefCount <> 0 then
    Error(reInvalidPtr);  
end;

其中 Error(reInvalidPtr)引发 EInvalidPointer 通过RTL本地的各种单位范围方法。

Where Error(reInvalidPtr) raises EInvalidPointer through a variety of unit-scoped methods local to the RTL.

如果我在编写自己的类,我应该实现 BeforeDestruction 吗?为什么不这样做呢? :

If I'm writing my own class, how should I implement BeforeDestruction? Why not do this? :

procedure TMyInterfacedObject.BeforeDestruction;
begin
  if RefCount <> 0 then
    raise EInvalidPointer.CreateRes(@SInvalidPointer) at ReturnAddress;
end;

全局 InvalidPointer 有什么特别之处吗? SysUtils 中声明的异常对象?如果这个 想法不好,在这里简单地引发自定义异常是否明智?

Is there something special with the global InvalidPointer exception object declared in SysUtils? If this is a bad idea, would it be sensible to simply raise a custom exception here?

推荐答案

大卫的答案的补充 InvalidPointer 有什么特别之处,它用于引发 EInvalidPointer 以及 OutOfMemory <-> EOutOfMemory 的详细信息,请参见 EHeapException

Complementary to David's answer; what's special about InvalidPointer, which is used to raise an EInvalidPointer, together with OutOfMemory <-> EOutOfMemory is explained in more detail in the documentation topic for their ascendant EHeapException:


EHeapException 是与堆分配的内存相关的错误的异常类。

EHeapException is the exception class for errors related to heap-allocated memory.

EHeapException 的后代EOutOfMemory和EInvalidPointer用于
来处理动态内存分配失败和无效的指针
操作。

EHeapException's descendants—EOutOfMemory and EInvalidPointer—are used to handle failed allocations of dynamic memory and invalid pointer operations.

注意:只要应用程序正在运行
,它们就会在应用程序启动时预先分配用于这些异常的内存,并保持分配状态。永远不要直接提出 EHeapException 或其后代。

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.

我想这是不安全的一旦遇到内存问题,就分配内存来创建这些错误:因为内存不足或可能损坏...

Which amounts to I guess is, it may not be safe to allocate memory for creating these errors once you have problems with memory: for lack of it or possible corruption...

这篇关于为什么不提高EInvalidPointer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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