Recent

Author Topic: [Solved] Defining multiple classes in TCustomApplication  (Read 292 times)

nikel

  • Sr. Member
  • ****
  • Posts: 252
[Solved] Defining multiple classes in TCustomApplication
« on: April 17, 2025, 07:55:57 am »
Hello, I'm trying to learn oop and publish some programs that can be easily developed. I tried the code below but got error: External Sigsegv at Application.Run. Here's my code.

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}{$J-}
  4. {$typedaddress on}
  5. {$m+}
  6.  
  7. uses
  8.   {$IFDEF UNIX}
  9.   cthreads,
  10.   {$ENDIF}
  11.   Classes, SysUtils, CustApp
  12.   { you can add units after this };
  13.  
  14. type
  15.  
  16.   { TMain }
  17.  
  18.   TMain = class(TCustomApplication)
  19.   protected
  20.     procedure DoRun; override;
  21.   public
  22.     constructor Create(TheOwner: TComponent); override;
  23.     destructor Destroy; override;
  24.     procedure WriteHelp; virtual;
  25.   end;
  26.  
  27.   type
  28.     TSong = class(TMain)
  29.     private
  30.       FPath: string;
  31.     protected
  32.       procedure DoRun; override;
  33.     public
  34.       property Path: string read FPath write FPath;
  35.     end;
  36.  
  37.   { TMain }
  38.  
  39. procedure TMain.DoRun;
  40. var
  41.   ErrorMsg  : string;
  42.  
  43.   S1        : TSong;
  44. begin
  45.   // quick check parameters
  46.   ErrorMsg:=CheckOptions('h', 'help');
  47.   if ErrorMsg<>'' then begin
  48.     ShowException(Exception.Create(ErrorMsg));
  49.     Terminate;
  50.     Exit;
  51.   end;
  52.  
  53.   // parse parameters
  54.   if HasOption('h', 'help') then begin
  55.     WriteHelp;
  56.     Terminate;
  57.     Exit;
  58.   end;
  59.  
  60.   { add your program here }
  61.  
  62.   WriteLn(S1.Path);
  63.  
  64.   // stop program loop
  65.   Terminate;
  66. end;
  67.  
  68. constructor TMain.Create(TheOwner: TComponent);
  69. begin
  70.   inherited Create(TheOwner);
  71.   StopOnException:=True;
  72. end;
  73.  
  74. destructor TMain.Destroy;
  75. begin
  76.   inherited Destroy;
  77. end;
  78.  
  79. procedure TMain.WriteHelp;
  80. begin
  81.   { add your help code here }
  82.   writeln('Usage: ', ExeName, ' -h');
  83. end;
  84.  
  85. procedure TSong.DoRun;
  86. begin
  87.   FPath:='/home/user/Desktop/song.mp3';
  88. end;
  89.  
  90. var
  91.   Application: TMain;
  92.  
  93. {$R *.res}
  94.  
  95. begin
  96.   Application:=TMain.Create(nil);
  97.   Application.Title:='project1';
  98.   Application.Run;
  99.   Application.Free;
  100. end.

How can I fix the error?
« Last Edit: April 17, 2025, 10:36:13 am by nikel »

Nimbus

  • New Member
  • *
  • Posts: 39
Re: Defining second class in TCustomApplication
« Reply #1 on: April 17, 2025, 10:25:31 am »
Hello,
You've declared S1 as TSong instance in a var section, but you never actually created an instance (by calling the constructor) before use. This is essential when working with classes - just declaring a variable is not enough. Normally the relevant code might be:

Code: Pascal  [Select][+][-]
  1. var
  2.   S1: TSong;
  3. begin
  4.   { ... }
  5.  
  6.   { add your program here }
  7.  
  8.   S1 := TSong.Create(Nil);  { constructor - always call before use }
  9.   S1.DoRun;
  10.   WriteLn(S1.Path);
  11.   S1.Free;  { destroy the instance when no longer needed }
  12.  

However, I'm not sure I fully understand what you are actually trying to achieve. For instance, I don't know why you'd want to inherit TSong class from TMain.

nikel

  • Sr. Member
  • ****
  • Posts: 252
Re: Defining multiple classes in TCustomApplication
« Reply #2 on: April 17, 2025, 10:35:53 am »
Thank you for your reply. That helped a lot.

 

TinyPortal © 2005-2018