[DUG] Variables stored

Paul Heinz paul at accredo.co.nz
Fri Jan 21 22:58:16 NZDT 2011


Alister wrote: 

> you might be able to go even one better by setting the Action 
> = caFree in OnClose and just use result := 
> Self.Create(nil).ShowModal = mrOK; removing the with, and 
> cutting the code from 6 lines to 1.

Even better, add a CloseAction property to your own TForm descendent and
override DoClose to default to that property value thus avoiding even
needing an OnClose handler. That's 0 lines of code :-) Although
technically property value assignments are just lines of code in another
(interpreted) guise.

Jolyon is right about not using Free and using Release instead. What
Release does is post a Windows message so that the call of Free on the
form instance is deferred until the main Wndproc is back processing
messages again by which time all event handlers are sure to have
completed. Calling Free may 'seem' to work sometimes but a lurking 'use
after Free' behaviour is only going to bite you in the ass eventually.

And using caFree is quite safe since, despite the name, it actually
calls Release (not Free) on the form instance.

Cheers,
  Paul.



More information about the Delphi mailing list