Friday, April 19, 2019

delphi - Why not raise EInvalidPointer?

The Delphi documentation states :




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




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;


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



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;


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?

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...