Recent

Author Topic: [SOLVED] CreateThread + String/Dynamic array inside thread proc = SIGSEGV crash  (Read 5302 times)

Syndrome

  • New Member
  • *
  • Posts: 35
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
« Last Edit: January 20, 2015, 08:43:20 am by Syndrome »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: CreateThread + String/Dynamic array inside thread proc = Instant crash
« Reply #1 on: October 15, 2014, 04:27:26 am »
I think you need to use stdcall for calling convention.

Syndrome

  • New Member
  • *
  • Posts: 35
Re: CreateThread + String/Dynamic array inside thread proc = Instant crash
« Reply #2 on: October 15, 2014, 04:33:34 am »
stdcall didnt help. I still cant compile this code. Application silently crashes before form1 is displayed
« Last Edit: October 15, 2014, 05:40:45 am by Syndrome »

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: CreateThread + String/Dynamic array inside thread proc = Instant crash
« Reply #3 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;

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: CreateThread + String/Dynamic array inside thread proc = Instant crash
« Reply #4 on: October 15, 2014, 12:50:27 pm »
cthreads included on Linux/MacOS? Otherwise try BeginThread() instead of CreateThread().
Lazarus 1.7 (SVN) FPC 3.0.0

Syndrome

  • New Member
  • *
  • Posts: 35
Re: CreateThread + String/Dynamic array inside thread proc = Instant crash
« Reply #5 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;
« Last Edit: January 20, 2015, 08:40:06 am by Syndrome »

 

TinyPortal © 2005-2018