Error on close executable

Asked

Viewed 854 times

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 ok, but in case she presses the close button .

inserir a descrição da imagem aqui

Displays the following message:

inserir a descrição da imagem aqui

It crashes, appears the windows message to close the program, and just so it closes.

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

I believe you’re having a problem when it comes to releasing the Foms, where I may be missing?

My intention is that when you click on button3 had a reset in the form, and the user starts from scratch.

  • You in the OnClose form is erasing itself, having recreated, and giving Application.Run again?

  • I believe not, when I say click on the close is the same window. I didn’t add any event to onclose

  • The code you showed is what’s in the .dpr then?

  • Exactly, I use it on a button.

  • What do you mean a button? Can you show more of the code? It’s too confusing for me yet.

  • I’ll edit the question friend.

  • In which line does the problem happen?

Show 2 more comments

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;

1

At startup, the application created the forms and kept reference to them in order to be able to destroy them later, when it was closed.

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

The solution might be to remove the forms from the automatic startup list, maybe change the way to "start again" the application.

If you really want to reset the application, you can use this code: https://stackoverflow.com/a/6020898/1274092. It creates a new application instance and closes the current instance then.

  • what I want to do is that the flow continues, as if the application had been closed and opened again a "reset" in the application.

  • @Guilhermelima is not enough information. Maybe you should take over the execution flow of the application by creating and controlling the lifecycle of the forms. The call to Application.run in a button click is also pretty weird.

  • I agree, and I’m willing to accept a new way of doing what I want, which again I say, is basically to reset the application.

  • @Guilhermelima I added a reference that restarts the actual application (open a new instance and close the current instance then).

  • Note that this code "reset" the application creates a new application instance, which prevents you from using a Debugger in the new instance.

  • @Darkhyudra does not prevent no. Using the debug button is not the only way to debug in Delphi. You can also attach the debugger to an existing process and also indicate a program that will run before yours when you have the IDE debug. And maybe there are still other ways I can’t remember.

  • @Caffé but for this the command that you need to pass to Shell is different. I did not say that it is impossible, it’s just that with that command, the program will not go through the Delphi debugger.

  • @Darkhyudra Well, I don’t think I understand what you’re talking about. I just don’t understand any problem how debugging, both the current instance and the new instance, is perfectly feasible and simple to debug any of them, in any desirable situation.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.