Lazarus

Free Pascal => General => Topic started by: Syndrome on October 15, 2014, 02:37:11 am

Title: [SOLVED] CreateThread + String/Dynamic array inside thread proc = SIGSEGV crash
Post by: Syndrome on October 15, 2014, 02:37:11 am
Create new project, double click your form1 and paste this code:

Code: [Select]
function SilentCrash( dummy:pointer ):dword; stdcall;
var
   s:string;
begin
   while true do begin
      s:= 'asd';
      sleep(10000);
   end;
end;

procedure TForm1.FormCreate(Sender:TObject);
var
   asd:dword;
begin
   CreateThread( nil, 0, @SilentCrash, nil, 0, asd);
end;

silent crash example 2:
Code: [Select]
function SilentCrash( dummy:pointer ):dword; stdcall;
var
   d:array of byte;
begin
   while true do begin
      setlength(d,10);
      d[3]:= 2;
      sleep(10000);
   end;
end;

procedure TForm1.FormCreate(Sender:TObject);
var
   asd:dword;
begin
   CreateThread( nil, 0, @SilentCrash, nil, 0, asd);
end;

Can anyone check this code?
Dont forget to include windows unit.
i cant use ansistrings and dynamic arrays outside main thread due to instant crashes.

if someone can compile examples above under win 8.1 x64 please answer this thread.
sorry for my awful english

my specs: win 8.1 x64 / lazarus 1.2.6 x32 / A10-4600M
Title: Re: CreateThread + String/Dynamic array inside thread proc = Instant crash
Post by: engkin on October 15, 2014, 04:27:26 am
I think you need to use stdcall for calling convention.
Title: Re: CreateThread + String/Dynamic array inside thread proc = Instant crash
Post by: Syndrome on October 15, 2014, 04:33:34 am
stdcall didnt help. I still cant compile this code. Application silently crashes before form1 is displayed
Title: Re: CreateThread + String/Dynamic array inside thread proc = Instant crash
Post by: BrunoK on October 15, 2014, 11:57:55 am
Try moving it to TForm.OnShow like :

Code: [Select]
  TForm1 = class(TForm)
  .
  .
  private
     FShownOnce:boolean;
  end;
.
.

procedure TForm1.FormShow(Sender: TObject);
var
  asd: dword;
begin
  if not FShownOnce then begin
    FShownOnce:=True;
    CreateThread(nil, 0, @SilentCrash, nil, 0, asd);
  end;
end;
Title: Re: CreateThread + String/Dynamic array inside thread proc = Instant crash
Post by: Ocye on October 15, 2014, 12:50:27 pm
cthreads included on Linux/MacOS? Otherwise try BeginThread() instead of CreateThread().
Title: Re: CreateThread + String/Dynamic array inside thread proc = Instant crash
Post by: Syndrome on October 15, 2014, 10:51:27 pm
Thanks everyone for their help.
lazarus doesnt support windows.createthread function
here is solution for threads:
Code: [Select]
function SecondThread( dummy:pointer ):ptrint;
var
   s:string;
   d:array of byte;
begin
   while true do begin
      s:= 'asd'+inttostr(222);
      setlength(d,10);
      d[4]:= d[2];
      sleep(10000);
   end;
end;

procedure TForm1.FormCreate(Sender:TObject);
begin
   beginthread(@secondthread,nil);
end;
TinyPortal © 2005-2018