I have a form dynamically created that needs to be destroyed:
- later (at application shutdown) IF
AssignDatasucceeds, or - immediately in case
AssignDatacrashes.
Since the form is owned by the application, it is not safe to just call FreeAndNil on it. But Close also won’t work; it won’t close the window.
FrmLoader:= TFrmLoader.Create(Application); <----- Application is the Owner
TRY
FrmLoader.AssignData(FileData);
EXCEPT
FreeAndNil(FrmLoader); // <------ unsafe
FrmLoader.Close; // <------ this is the safe method to close it but won't work
RAISE;
END;
FrmLoader.DoStuff;
It is perfectly safe to call
Freeon a form that is owned. Or pass the form toFreeAndNil. And that is the right way to dispose of your form.So, this code does what you need.