This post continues the series of The Delphi Language Chapter teasers from Jon Shemitz’ .NET 2.0 for Delphi Programmers book. Last time we covered class helpers and why they were invented. This post looks in more detail at the TObjectHelper class helper and the Free method.
Note that I do not get any royalties from the book and I highly recommend that you get your own copy – for instance at Amazon.
"The try-finally-Free pattern
The Borland.Delphi.System unit declares the TObjectHelper class helper that injects methods like Free and ClassName into all classes (including FCL classes). The Free method is special and implements deterministic disposing of unmanaged resources by calling IDispose.Disposable (if it is implemented by the class).
if (Self <> nil) and (Self is IDisposable) then
(Self as IDisposable).Dispose;
To complete the cycle, the compiler effectively transforms an overridden Destroy destructor into an implementation of the IDisposable interface.
TFoo = class
destructor Destroy; override;
This is transformed by the compiler into the equivalent of
TFoo = class(TObject, IDisposable)
procedure IDisposable.Dispose = Destroy;
This means that the traditional Delphi pattern of calling Free to clean up and writing a destructor to implement the clean up will have the same semantics in .NET. The difference is that actually deallocating the memory allocated by objects is deferred to the garbage collector. It can be argued that if all your destructors do is freeing the memory of other nested objects, calling Free and implementing destructors in .NET is pure overhead. The counter-argument is that if any of the sub-objects implements a resource releasing IDisposable.Dispose (now or in the future), this is the correct way of doing things. "