Donovan wrote:
> Given TForm1, TForm2 (TForm1 is the main form and TForm2 is NOT
> automatically created) with the following code:
>
> Instance1:
>
> No code in form2 and the following works as expected. Form2 is shown,
> closed.
>
> procedure TForm1.Button1Click(Sender: TObject);
> begin
> try
> Application.CreateForm(TForm2, Form2);
> Form2.Show;
> Sleep(2000);
> Form2.Close;
> Sleep(2000);
> //ShowMessage('Hello');
> Form2.Caption := 'I was never freed';
> Form2.Show;
> except
> on E : Exception do
> ShowMessage(E.Message);
> end;
> end;
>
> Instance2:
>
> procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
> begin
> Action := caFree;
> end;
>
> TForm1 code from above. Why do I not get an access violation when
> I attempt
> to set the caption? The close event has triggered and the action
> should have
> freed the form. Put a showmessage PRIOR to the caption being set
> and you get
> an access violation. Can someone explain this to me?
Using caFree causes Form.Release to be called during Close. Now Form.Release
does not immediately call Destroy, rather it posts a message into the
windows message queue to call Form.Destroy later.
This is done so that Release can be called inside a method of the Form to be
destroyed. So, until you actually enter a message loop or call
Application.ProcessMessages, Form2 still exists.
Now, guess what ShowMessage does? Yes, it enters a message loop so Form2
_will_ be destroyed by the time you set Form2.Caption.
TTFN,
Paul.
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz