Error closing executable

2

I have an application that has 4 forms.

After the process there is a button for the person to start again.

procedure TForm4.Button3Click(Sender: TObject);
begin
      FreeAndNil(Form1);
      FreeAndNil(Form2);
      FreeAndNil(Form3);
      FreeAndNil(Form4); //Libera o form da memória
      Application.CreateForm(Tform1, form1);
      Application.CreateForm(Tform2, form2);
      Application.CreateForm(Tform3, form3);
      Application.CreateForm(Tform4, form4);
      Application.Run; //Roda a aplicação
end;

Until then, but if she presses the close button.

Displaysthefollowingmessage:

It crashes, the windows message appears to close the program, and only then does it close.

obs: (The error only appears if the person clicks the button to start the process again, if it opens the program for the first time, it works correctly.)

I think you have a problem releasing the forms, where can I be wrong?

My intention is that when I click on button3 there would be a reset on form, and the user would start from scratch.

    
asked by anonymous 27.01.2016 / 13:49

2 answers

2

Try this:

procedure TForm4.Button3Click(Sender: TObject);
begin
 if TForm1 = nil then Form1 := TForm1.Create(Application);
 Form1.Show;

 if TForm2 = nil then Form2 := TForm2.Create(Application);
 Form2.Show;

 if TForm3 = nil then Form3 := TForm3.Create(Application);
 Form3.Show;

 if TForm4 = nil then Form4 := TForm4.Create(Application);
 Form4.Show;
end;

// insira no evento OnClose de cada Form1,2,3,4
procedure TForm4.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 action:=caFree;
 Form4 :=nil;
end;
    
28.01.2016 / 15:38
1

At startup, the application created the forms and saved them in order to be able to destroy them after it was shut down.

The Button3Click code destroys the forms created by the application, so that when the application tries to destroy them (during shutdown) it calls free using the references , now invalid, to forms that no longer exist, hence the error.

The solution might be to remove the forms from the auto-start list, perhaps change the way to "start the application again."

If you really want to reset the application, you can use this code: link . It creates a new instance of the application and then exits the current instance.

    
27.01.2016 / 16:13