Jump to content
Ian Branch

Freeing Show v ShowModal??

Recommended Posts

Hi Team,

D11.1.1.

I have used the following construct in the calling/main form extensively..

  //
  ChangesLogForm := TChangesLogForm.Create(self);
  //
  try
    ChangesLogForm.ShowModal;
  finally
    DBC1.CloseDataSets;
    FreeAndNil(ChangesLogForm);
  end;
  //

for showing Modal forms.

 

I now need to show several forms at the same time so I moved to this construct..

  //
  ChangesLogForm := TChangesLogForm.Create(self);
  //
  ChangesLogForm.Show;
  //

and added 'Action := caFree;' in the OnClose of the forms.

 

Is this safe/correct/acceptable?

Are there any -ve consequences?

 

Regards & TIA,

Ian

Share this post


Link to post

It's quite possible there will be consequences. Impossible to say without knowing what your code looks like. Perhaps there are dependencies that require forms to be destroyed in a particular order. The erroneous finally block where you call that CloseDataSets method as well as destroying a form isn't a great sign. 

Share this post


Link to post

If you are just asking about "'Action := caFree;", yes it's fine. Otherwise what David said. Non-modal needs different logic.

Share this post


Link to post

Thank you David & Attila,

I just want to be sure.  :-)

The CloseDataSets  was valid in the original code where only one sub-form was open at a time, however it isn't appropriate where multiple forms are going to be open.  The relevant dataset is closed in the sub-form before the actual Close.

 

Regards & Tks again,

Ian

Share this post


Link to post
1 hour ago, Ian Branch said:

The CloseDataSets  was valid in the original code where only one sub-form was open at a time

It was never correct to put it in the same finally as lifetime management. Of course it won't matter if it never throws an exception. But it's basic sound practise not to put multiple unrelated tasks in a finally block. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×