Frank Heckenbach wrote:
Yes, the two `Dispose's on the same value are a disaster
For that very reason, it is a good idea to call Dispose wrapped:
procedure MyDispose( var thePtr: Pointer); begin if thePtr <> nil then begin Dispose( thePtr); thePtr:= nil end end;
Of course, if the pointer-value (itself) has been copied, it can still be diposed twice. To prevent that, you need a well designed program structure in which it is clear who owns the pointer.
The above procedure makes memory management easier also.
begin thePtr:= nil;
....
MyDispose( thePtr) end;
If .... is a complicated sequence of conditional statements, it may be that New( thePtr) isn't called. Still, memory management will be correct.
Finally, as a special bonus, you get a runtime error when you dereference the MyDisposed pointer, as it has a NIL value.
Regards,
Adriaan van Os