Reputation: 21124
I have an MDI application in which I have a MDI child doing some processing (needs 10-20 seconds to finish). I have discovered that if I close the main application, it crashes because the MDI child is closed to early (before finishing its processing).
The code is like this (it is way too complicated to put all code in here):
mainForm.OnButtonClick
begin
start data processing; <--- 10 seconds
create MDI child; <--- instant
create visual controls (runtime) <==
display the processed data in MDI child; <== 1 sec
end;
The program crashes on the 3rd line. FastMM says that "FastMM has detected an attempt to call a virtual method on a freed object". Obviously the MDI child has been freed.
How to prevent this?
Upvotes: 0
Views: 445
Reputation: 108929
In your MDI child window, do
procedure TForm2.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := not IsWorking;
end;
and do
mainForm.OnButtonClick
begin
IsWorking := true;
try
start data processing; <--- 10 seconds
create MDI child; <--- instant
create visual controls (runtime) <==
display the processed data in MDI child; <== 1 sec
finally
IsWorking := false;
end;
end;
Upvotes: 5