Recent

Author Topic: How to use classes and forms together  (Read 544 times)

nikel

  • Sr. Member
  • ****
  • Posts: 278
How to use classes and forms together
« on: April 30, 2026, 09:43:50 pm »
Hello, I'm trying to create and free my class. In the main lpr file I haver this code:
Code: Pascal  [Select][+][-]
  1. {$R *.res}
  2.  
  3. begin
  4.   RequireDerivedFormResource:=True;
  5.   Application.Scaled:=True;
  6.   Application.Initialize;
  7.   // Application.CreateForm(TForm1, Form1);
  8.  
  9.   D:=TDownload.Create(nil);
  10.   D.DoRun;
  11.  
  12.   Application.Run;
  13.  
  14.   D.Free;
  15. end.

It never reaches D.Free when I debug my program. I tried to place the line to different places however it caused other problems. How can use forms and classes together?

Modify:

Here's how I declare my class:
Code: Pascal  [Select][+][-]
  1.   TDownload = class(TForm1)
  2.   private
  3.     LinkToFile    : string;
  4.     TaskToKill    : shortstring;
  5.   public
  6.     constructor Create(AOwner: TComponent); reintroduce;
  7.     constructor Create(AOwner: TComponent; L: string; T: shortstring); reintroduce;
  8.     destructor Destroy; override;
  9.  
  10.     procedure SetLinkToFile(L: string);
  11.     function GetLinkToFile(): string;
  12.     ...
« Last Edit: May 01, 2026, 12:34:20 pm by nikel »

ASerge

  • Hero Member
  • *****
  • Posts: 2494
Re: How to use classes with forms
« Reply #1 on: April 30, 2026, 10:35:17 pm »
It never reaches D.Free when I debug my program. I tried to place the line to different places however it caused other problems. How can use forms and classes together?
Comment out Application.Run or add Application.Terminate; before that.

nikel

  • Sr. Member
  • ****
  • Posts: 278
Re: How to use classes with forms
« Reply #2 on: May 01, 2026, 12:29:09 pm »
Thanks for the reply. I tried commenting out Application.Run, this time the form didn't work correctly.

ASerge

  • Hero Member
  • *****
  • Posts: 2494
Re: How to use classes with forms
« Reply #3 on: May 01, 2026, 01:23:05 pm »
Thanks for the reply. I tried commenting out Application.Run, this time the form didn't work correctly.
If you have a regular form, then why not just use:
Code: Pascal  [Select][+][-]
  1. begin
  2.   RequireDerivedFormResource:=True;
  3.   Application.Scaled:=True;
  4.   Application.Initialize;
  5.   // Application.CreateForm(TForm1, Form1);
  6.   Application.CreateForm(TDownload, D);
  7.   Application.Run;
  8. end.

nikel

  • Sr. Member
  • ****
  • Posts: 278
Re: How to use classes and forms together
« Reply #4 on: May 01, 2026, 01:57:01 pm »
ASerge, thanks again.  I fixed the problem with this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Close_PnlMouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. begin
  4.   with TPanel(Sender) do
  5.   begin
  6.     BevelOuter:=bvNone;
  7.     BevelInner:=bvLowered;
  8.   end;
  9.  
  10.   if not (D = nil) then
  11.   begin
  12.     D.Free;
  13.   end;
  14.   Application.Terminate;
  15. end;

ASerge

  • Hero Member
  • *****
  • Posts: 2494
Re: How to use classes and forms together
« Reply #5 on: May 01, 2026, 04:04:58 pm »
The Free method does not nil the object reference.
Better:
Code: Pascal  [Select][+][-]
  1. if Assigned(D) then
  2.   FreeAndNil(D);
or even simple
Code: Pascal  [Select][+][-]
  1. FreeAndNil(D);

cdbc

  • Hero Member
  • *****
  • Posts: 2787
    • http://www.cdbc.dk
Re: How to use classes and forms together
« Reply #6 on: May 01, 2026, 04:26:38 pm »
Hi
Caution with:
Code: Pascal  [Select][+][-]
  1. FreeAndNil(D);
It will crash if 'D' is nil...!
Code: Pascal  [Select][+][-]
  1. if Assigned(D) then FreeAndNil(D);
...is the safer bet  8)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

ASerge

  • Hero Member
  • *****
  • Posts: 2494
Re: How to use classes and forms together
« Reply #7 on: May 01, 2026, 04:32:11 pm »
It will crash if 'D' is nil...!
No. TObject(nil).Free; work fine.

cdbc

  • Hero Member
  • *****
  • Posts: 2787
    • http://www.cdbc.dk
Re: How to use classes and forms together
« Reply #8 on: May 01, 2026, 05:07:16 pm »
Hi
Yes, but that is not what you are doing!
'FreeAndNil' doesn't check for nil  %)
Free does.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

ASerge

  • Hero Member
  • *****
  • Posts: 2494
Re: How to use classes and forms together
« Reply #9 on: May 01, 2026, 06:55:27 pm »
Hi
Yes, but that is not what you are doing!
'FreeAndNil' doesn't check for nil  %)
FreeAndNil simply calls the Free method, which performs the check. And then sets the reference to nil. It is safe to call FreeAndNil with nil.

 

TinyPortal © 2005-2018