Recent

Author Topic: [SOLVED] Error class 'external : SIGSEGV' when running Thread  (Read 5349 times)

incendio

  • Sr. Member
  • ****
  • Posts: 269
Hi guys,

I have thread declaration in mainunit.pas
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   { TMyThread }
  4.  
  5.   TMyThread = class(TThread)
  6.   private
  7.     fStatusText: string;
  8.     procedure ShowStatus;
  9.   protected
  10.     procedure Execute; override;
  11.   public
  12.     constructor Create(CreateSuspended: boolean);
  13.   end;
  14.  
  15.   { D3 }
  16.   TD3GetDtThr = class(TThread)
  17.   protected
  18.     procedure Execute; override;
  19.   public
  20.     constructor Create(CreateSuspended: boolean);
  21.   end;
  22.  
  23.   TMainFrm = class(TForm)
  24.     btnStart: TButton;
  25.     Label1: TLabel;
  26.     Label2: TLabel;
  27.     Label3: TLabel;
  28.     Label4: TLabel;
  29.     Label5: TLabel;
  30.     Label6: TLabel;
  31.     Label7: TLabel;
  32.     Label8: TLabel;
  33.     lblThr: TLabel;
  34.     lblStTm: TLabel;
  35.     lblTmDf: TLabel;
  36.     P0: TProgressBar;
  37.     P1: TProgressBar;
  38.     P2: TProgressBar;
  39.     P3: TProgressBar;
  40.     P4: TProgressBar;
  41.     P5: TProgressBar;
  42.     procedure btnStartClick(Sender: TObject);
  43.     procedure FormCreate(Sender: TObject);
  44.     procedure ThreadDone(Sender: TObject);
  45.     procedure Go();
  46.   private
  47.   public
  48.   end;
  49.  
  50. procedure TMainFrm.ThreadDone(Sender: TObject);
  51. begin
  52.   ThreadsRunning := ThreadsRunning-1;
  53.   P0.StepIt;
  54.  
  55.   if (ThreadsRunning > 0) then
  56.   begin
  57.     lblThr.Caption:= IntToStr(ThreadsRunning);
  58.     lblTmDf.Caption := IntToStr(SecondsBetween(StartTime,Now));
  59.   end;
  60. end;
  61.  

Procedure for TD3GetDtThr
Code: Pascal  [Select][+][-]
  1. constructor TD3GetDtThr.Create(CreateSuspended: boolean);
  2. begin
  3.   FreeOnTerminate := True;
  4.   inherited Create(CreateSuspended);
  5. end;
  6.  
  7. procedure TD3GetDtThr.Execute;
  8. begin
  9. end;
  10.  

Code to start the thread
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.Go();
  2. var
  3.   Dt5 : TD3GetDtThr;
  4. begin
  5.   Dt5 := Dt5.Create(False);
  6.   Dt5.OnTerminate:= @ThreadDone;
  7.   Dt5.Start;
  8. end;
  9.  

When procedure Go() called, got the error
class 'external : SIGSEGV'
in file mainunit.pas line 197
FreeOnTerminate := True;

That error point to constructor TD3GetDtThr.Create

What is wrong with the codes?

Thanks in advance.
« Last Edit: June 15, 2019, 09:40:28 am by incendio »

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Error class 'external : SIGSEGV' when running Thread
« Reply #1 on: June 15, 2019, 08:46:29 am »
You create the thread with createsuspended false.
That means the thread already runs.
After that you call start on a running thread!

I usually do not recommend it, but in this case you can use CreateSuspended= true, then call start when everything is set up.
Report back if that doesn't solve the issue: this was eyes only and I did not test.
« Last Edit: June 15, 2019, 08:51:22 am by Thaddy »
Specialize a type, not a var.

incendio

  • Sr. Member
  • ****
  • Posts: 269
Re: Error class 'external : SIGSEGV' when running Thread
« Reply #2 on: June 15, 2019, 09:05:16 am »
I have tried this code
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.Go();
  2. var
  3.   Dt5 : TD3GetDtThr;
  4. begin
  5.   Dt5 := Dt5.Create(false);
  6.   Dt5.OnTerminate:= @ThreadDone;
  7.   //Dt5.Start;
  8. end;
  9.  

and this code
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.Go();
  2. var
  3.   Dt5 : TD3GetDtThr;
  4. begin
  5.   Dt5 := Dt5.Create(True);
  6.   Dt5.OnTerminate:= @ThreadDone;
  7.   Dt5.Start;
  8. end;
  9.  

Still got the same error.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Error class 'external : SIGSEGV' when running Thread
« Reply #3 on: June 15, 2019, 09:22:15 am »
Can you post a project that shows the error?

incendio

  • Sr. Member
  • ****
  • Posts: 269
Re: Error class 'external : SIGSEGV' when running Thread
« Reply #4 on: June 15, 2019, 09:29:22 am »
Can you post a project that shows the error?
What do you mean?

The error point to this procedure, on the line FreeOnTerminate := True;
Code: Pascal  [Select][+][-]
  1. constructor TD3GetDtThr.Create(CreateSuspended: boolean);
  2. begin
  3.   FreeOnTerminate := True;
  4.   inherited Create(CreateSuspended);
  5. end;        

incendio

  • Sr. Member
  • ****
  • Posts: 269
Re: Error class 'external : SIGSEGV' when running Thread
« Reply #5 on: June 15, 2019, 09:40:13 am »
Found the problem!

The error caused by error in thread declaration.

This is the error declaration
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.Go();
  2. var
  3.   Dt5 : TD3GetDtThr;
  4. begin
  5.   Dt5 := Dt5.Create(false);
  6.   Dt5.OnTerminate:= @ThreadDone;
  7. end;            

It should be like this
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.Go();
  2. var
  3.   Dt5 : TD3GetDtThr;
  4. begin
  5.   Dt5 := TD3GetDtThr.Create(false);
  6.   Dt5.OnTerminate:= @ThreadDone;
  7. end;            

It can compile cause Lazarus doesn't point to that declaration.

Thanks anyway for your help.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: [SOLVED] Error class 'external : SIGSEGV' when running Thread
« Reply #6 on: June 15, 2019, 10:17:39 am »
There is still a basic error:
You should call the inherited first in the constructor, then the housekeeping. The inherited might reset things.....
Specialize a type, not a var.

incendio

  • Sr. Member
  • ****
  • Posts: 269
Re: [SOLVED] Error class 'external : SIGSEGV' when running Thread
« Reply #7 on: June 15, 2019, 12:42:56 pm »
I copied Lazarus example that called inherited last.

I will change though as your suggestion. Thanks.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Error class 'external : SIGSEGV' when running Thread
« Reply #8 on: June 15, 2019, 09:51:13 pm »
This is the error declaration
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.Go();
  2. var
  3.   Dt5 : TD3GetDtThr;
  4. begin
  5.   Dt5 := TD3GetDtThr.Create(false);
  6.   Dt5.OnTerminate:= @ThreadDone;
  7. end;
You found the obvious error, but in vain ignored what Thaddy said. In code above the sixth line of code can be executed after the thread is finished and freed. Make all settings in the constructor or call with Suspended = True.

incendio

  • Sr. Member
  • ****
  • Posts: 269
Re: Error class 'external : SIGSEGV' when running Thread
« Reply #9 on: June 16, 2019, 01:51:32 pm »
You found the obvious error, but in vain ignored what Thaddy said. In code above the sixth line of code can be executed after the thread is finished and freed. Make all settings in the constructor or call with Suspended = True.

I don't use constructor anymore.

Here are the codes :
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.Go();
  2. var
  3.   Dt1 : TLocGetDtThr1;
  4.   Dt6 : TLocGetDtThr2;
  5.   Dt2 : TDptGetDtThr;
  6.   Dt3 : TRtlGetDtThr;
  7.   Dt4 : TAssGetDtThr1;
  8.   Dt7 : TAssGetDtThr2;
  9.   Dt5 : TD3GetDtThr;
  10. begin
  11.   Dt1                := TLocGetDtThr1.Create( False);
  12.   Dt1.FreeOnTerminate:= True;
  13.   Dt1.OnTerminate    := @ThreadDone;
  14.  
  15.   Dt6                := TLocGetDtThr2.Create( False);
  16.   Dt6.FreeOnTerminate:= True;
  17.   Dt6.OnTerminate    := @ThreadDone;
  18.  
  19.   Dt2                := TDptGetDtThr.Create(False);
  20.   Dt2.FreeOnTerminate:= True;
  21.   Dt2.OnTerminate    := @ThreadDone;
  22.  
  23.   Dt3                := TRtlGetDtThr.Create(False);
  24.   Dt3.FreeOnTerminate:= True;
  25.   Dt3.OnTerminate    := @ThreadDone;
  26.  
  27.   Dt4                := TAssGetDtThr1.Create(False);
  28.   Dt4.FreeOnTerminate:= True;
  29.   Dt4.OnTerminate    := @ThreadDone;
  30.  
  31.   Dt7                := TAssGetDtThr2.Create(False);
  32.   Dt7.FreeOnTerminate:= True;
  33.   Dt7.OnTerminate    := @ThreadDone;
  34.  
  35.   Dt5                := TD3GetDtThr.Create(False);
  36.   Dt5.FreeOnTerminate:= True;
  37.   Dt5.OnTerminate    := @ThreadDone;
  38. end;
  39.  

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Error class 'external : SIGSEGV' when running Thread
« Reply #10 on: June 17, 2019, 03:49:13 am »
I don't use constructor anymore.
All the more need "Suspended = True".

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Error class 'external : SIGSEGV' when running Thread
« Reply #11 on: June 17, 2019, 08:09:45 am »
I don't use constructor anymore.
All the more need "Suspended = True".
I would still use the constructor, but like so:
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.Go();
  2. var
  3.   Dt5 : TD3GetDtThr;
  4. begin
  5.   Dt5 := TD3GetDtThr.Create(false);
  6. end;    
  7.  
  8. constructor TD3GetDtThr.Create(CreateSuspended: boolean); // Set to true or false, see below
  9. begin
  10.   inherited Create(true); // Always call inherited first. MUST be true, so you can also set it directly to true here
  11.   FreeOnTerminate := True;
  12.   Dt5.OnTerminate:= @ThreadDone; // Can do that here
  13.  If CreateSuspended = false then Start;  // Now it starts  immediately after setup if CreateSuspended = false and manually when true
  14. end;  
     
« Last Edit: June 17, 2019, 08:19:44 am by Thaddy »
Specialize a type, not a var.

incendio

  • Sr. Member
  • ****
  • Posts: 269
Re: Error class 'external : SIGSEGV' when running Thread
« Reply #12 on: June 17, 2019, 08:15:10 am »
I don't use constructor anymore.
All the more need "Suspended = True".
I would still use the constructor, but like so:

What is the benefit using constructor instead of not using it?

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: [SOLVED] Error class 'external : SIGSEGV' when running Thread
« Reply #13 on: June 17, 2019, 08:43:06 am »
As you can see, all house keeping / setup is done on a central place in the constructor.
You can subsequently run the thread with suspended = false as you intended, because of the way I set up the thread.
It is rather obvious: It prevents you from modifying a running thread! Which is essential.
If you look at my code you see I solved both your mistakes:
1. Inherited first, then the properties
2. You can now create the thread with suspended = false in a safe manner, because during the setup the thread is not started yet. and it still starts immediately after create.
« Last Edit: June 17, 2019, 08:47:14 am by Thaddy »
Specialize a type, not a var.

incendio

  • Sr. Member
  • ****
  • Posts: 269
Re: [SOLVED] Error class 'external : SIGSEGV' when running Thread
« Reply #14 on: June 17, 2019, 01:18:37 pm »
Ok, thanks for your help, I will change the codes.

 

TinyPortal © 2005-2018