Recent

Author Topic: [SOLVED] Exception class 'External: SIGSEGV'  (Read 3103 times)

Adri

  • New Member
  • *
  • Posts: 23
[SOLVED] Exception class 'External: SIGSEGV'
« on: April 27, 2019, 11:48:41 pm »
All,

Upon compiling a simple program I get a message:

Project project1 raised exception class 'External: SIGSEGV'.
In file 'unit1.pas' at line 47:
Memo1Lines.LoadFromFile(aFileName);

The code is simple enough, see below. What I try to do is to show the contents of a simple text file, which does exist on my system.
The form has nothing more then a button and a memo object. Apparently it goes wrong upon displaying the file content in the memo object; with line 47 marked as comment the application does run.
Can anyone tell me what I am doing wrong?  Should I better use a ListBox instead?
I also think the code after 'except' can be done in a better way. Any suggestions?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.   { TForm1 }
  12.   TForm1 = class(TForm)
  13.   Memo1   : TMemo;
  14.   btnClose: TEdit;
  15.   procedure btnCloseClick(Sender: TObject);
  16.  
  17.   private
  18.     { private declarations }
  19.   public
  20.     { public declarations }
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.   Memo1: TMemo;
  27.  
  28. implementation
  29. {$R *.lfm}
  30.  
  31. Procedure ProcessFile(aFileName:String);
  32.   var
  33.     MyFile      : Text;
  34.     Line        : String;
  35.     Counter, i  : Integer;
  36.     myStringList: TStringList;
  37.   begin
  38.     try
  39.       AssignFile(MyFile,aFileName);
  40.       {$I-}
  41.         Reset(MyFile);
  42.       {$I+}
  43.       If (IoResult = 0) Then
  44.       Begin
  45.         Counter:=1;
  46.         myStringList:=TStringList.Create;               //Create my StringList
  47.         Memo1.Lines.LoadFromFile(aFileName);
  48.         myStringList.LoadFromFile(aFileName);
  49.         for i := 0 to Memo1.Lines.Count-1 do
  50.           Memo1.Lines[i] := IntToStr(i) + ' ' + Memo1.Lines[i];
  51.         Inc(Counter);
  52.       end;
  53.       myStringList.Free;
  54.     except
  55.       on E: EInOutError do
  56.        writeln('File handling error occurred. Details: ', E.Message);
  57.     end;
  58.     CloseFile(MyFile)
  59.   end;
  60.  
  61. { TForm1 }
  62.  
  63. procedure TForm1.btnCloseClick(Sender: TObject);
  64. begin
  65.   Close;
  66. end;
  67.  
  68. begin
  69.   ProcessFile('D:\TestFile.txt');
  70. end.
  71.  
  72.  

« Last Edit: April 29, 2019, 07:29:45 pm by Adri »

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: Exception class 'External: SIGSEGV'
« Reply #1 on: April 28, 2019, 12:17:07 am »
That's because "ProcessFile" is not a method of your form, then can't find Memo1.

Another problem is you're calling ProcessFile outside the Form as well, better call it OnCreate event.

And finally you'r code seems too complex, I reduced it.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.   { TForm1 }
  12.   TForm1 = class(TForm)
  13.     btnClose: TEdit;
  14.   Memo1   : TMemo;
  15.   procedure btnCloseClick(Sender: TObject);
  16.   procedure FormCreate(Sender: TObject);
  17.  
  18.   private
  19.     { private declarations }
  20.   procedure ProcessFile(aFileName: string);
  21.   public
  22.     { public declarations }
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30. {$R *.lfm}
  31.  
  32. Procedure TForm1.ProcessFile(aFileName:String);
  33. begin
  34.   Memo1.Lines.LoadFromFile(aFileName);
  35. end;
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.btnCloseClick(Sender: TObject);
  40. begin
  41.   Close;
  42. end;
  43.  
  44. procedure TForm1.FormCreate(Sender: TObject);
  45. begin
  46.   ProcessFile('D:\TestFile.txt');
  47. end;
  48.  
  49. end.

I think you can extend it adding if FileExists(aFileName) then... else ShowMessage('File does not exists')
« Last Edit: April 28, 2019, 12:24:09 am by Lainz »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Exception class 'External: SIGSEGV'
« Reply #2 on: April 28, 2019, 02:32:43 am »
In fact, you're doing quite a lot of thngs wrong ... or "almost wrong" (which is a recipe for disaster).

This is closer to your code than Lainz's and more correct (or at least a little bit cleaner) than yours:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.   { TForm1 }
  12.   TForm1 = class(TForm)
  13.   Memo1   : TMemo;
  14.   btnClose: TEdit;
  15.   procedure FormCreate(Sender: TObject);
  16.   procedure btnCloseClick(Sender: TObject);
  17.  
  18.   private
  19.     { private declarations }
  20.     procedure ProcessFile(aFileName:String);
  21.   public
  22.     { public declarations }
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.   Memo1: TMemo;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. uses LazFileUtils;
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.ProcessFile(aFileName:String);
  39. var
  40.   {MyFile was used only to test if the file was readable;
  41.           replaced by a call to FileIsReadable()}
  42.   {Line was never used: deleted}
  43.   {Counter was used for nothing: deleted}
  44.   i  : Integer;
  45.   {myStringList was used for... nothing: deleted}
  46. begin
  47.   if FileIsReadable(aFileName) then
  48.   try
  49.     Memo1.Lines.LoadFromFile(aFileName);
  50.     for i := 0 to Memo1.Lines.Count-1 do
  51.       Memo1.Lines[i] := IntToStr(i) + ' ' + Memo1.Lines[i];
  52.   except
  53.     on E: EInOutError do
  54.       {writeln can't be safely used in a GUI app; replaced by ShowMessage}
  55.       ShowMessage('File handling error occurred. Details: ' + E.Message);
  56.   end;
  57. end;
  58.  
  59. procedure TForm1.FormCreate(Sender: TObject);
  60. begin
  61.   ProcessFile('D:\TestFile.txt');
  62. end;
  63.  
  64. procedure TForm1.btnCloseClick(Sender: TObject);
  65. begin
  66.   Close;
  67. end;
  68.  
  69. end.
« Last Edit: April 28, 2019, 02:36:49 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Adri

  • New Member
  • *
  • Posts: 23
Re: Exception class 'External: SIGSEGV'
« Reply #3 on: April 28, 2019, 08:52:48 am »
Guys (?), you’re great, many thanks for explaining and improving.  :D
Some ‘extra’ lines where there for future use, not implemented yet, but with your improvements as base I can continue (and sleep better).
This amateur can continue! Thanks again.    ::)

 

TinyPortal © 2005-2018