[Lazarus] Lazarus application crash. Is PostMessage thread safe?

Giuliano Colla giuliano.colla at fastwebnet.it
Tue Feb 9 23:37:52 CET 2016


Hi Lazarus guru's,

I have stumbled into a problem with a Lazarus application. At random 
times, from 5 minutes to several hours, it crashes suddenly, and silently.
The only way to get a clue has been to run it with gdb.

It turns out that the crash occurs because of a Segmentation Fault in 
QTextEngine, triggered by a PostMessage, but apparently the cause was an 
endless loop in a previous PostMessage, which was executed some 1723 
times. Attached the gdb stack trace (with 1722 identical items suppressed).

The scenario is a main thread using LCL, which is used, among other 
things not relevant here, to display information to the user, and 
another thread which communicates with a real-time process, and uses a 
PostMessage to wake up the main thread and make it show what required.

It appears that in some circumstances, the Application.ProcessMessages 
invoked in the main thread restarts the handling of the same message, 
thus creating and endless loop.

Is Application.ProcessMessages the vilain, or PostMessage or what?

Snippet of auxiliary thread code:


> procedure TSendRec.DoDisplayError;
> var
>   Box: integer;
> begin
>    ErrList[ErrPut] := ErrMsg;
>    inc(ErrPut);
>    if ErrPut > High(ErrList) then ErrPut := 0;
>    {$IFDEF USE_MESSAGES}
>    PostMessage(Form1.Handle,LM_ERROR,Integer(ErrPut),0);
>    {$ENDIF}
> end;

Snippet of main thread code:

> procedure TForm1.HandleError(var Msg: TLMessage);
> var
> .....
> begin
>   inc(ErrorCnt);
>   if ErrGet <> ErrPut then begin
>     ErrMsg := ErrList[ErrGet];
> .......
>     AnswerString := Format('UNIT %XH device %s ERROR
> %XH',[ErrMsg.yunit,Localize(NamUn[ErrMsg.device]),ErrMsg.ytype]);
>     ShowError(Self,Answerstring,BoxError[Box],Box);
>     inc(ErrGet);
>     if ErrGet > High(ErrList) then ErrGet := 0;
>     end;
> end;
>
> procedure TForm1.ShowError(Sender: TObject; errorstring: string;
> Berror: TBoxerror; ErrBox: Integer);
> var
> .......
> begin
> ..... (code to fill up Panel4)
>   Panel4.Show;
>   Panel4.BringToFront;
>   Application.ProcessMessages;
>   ResetBtn.SetFocus;
> end;

Thanks for any help

Giuliano


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PostMessage.log
Type: text/x-log
Size: 8152 bytes
Desc: not available
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160209/2e6a952f/attachment-0002.bin>


More information about the Lazarus mailing list