I have a form that I use to show some information for some seconds. Is it ok for the form to free itself? Can I start a timer in the constructor, and then call self.free in the timer-event? Or will this potentially lead to trouble?
In addition, with a form you can call Release
.
It sends a CM_RELEASE message to the form. As a reaction it calls Free. The advantage of release is that there are no messages left for the form which could result in a crash.
Close
will or will not close the form, depending on how you set the close action. But I was speaking about something else: (if it set to caFree) .Close
is as safe as .Release
. I mean it will not create the problems that .Free
will create. –
Krute You can make the form to free itself when it gets closed by the user or from code:
procedure TForm27.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TForm27.FormCreate(Sender: TObject);
begin
Timer1.Enabled := True;
end;
procedure TForm27.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
Close;
end;
Make sure you supply an owner in the constructor incase the application shutdowns and the form is not destroyed at the time. The owner will free the form before freeing itself.
I have a whole suite of objects that free themselves, and I've run various tests on them with no problems/leaks shown. A TForm might be more complex, but as long as Self.Free() is the last call made, you ought to be safe.
(For those wondering why on earth I have object that free themselves; I pass them around the system a lot, so I implemented by own reference counting scheme. When the last reference is released, so the object frees itself).
This is exactly what is done with Interfaces.
© 2022 - 2024 — McMap. All rights reserved.