Recent

Author Topic: Indy UDP crashes the application  (Read 2166 times)

vladbfg

  • New Member
  • *
  • Posts: 16
Indy UDP crashes the application
« on: December 26, 2024, 07:05:57 am »
Hi all.

There is project with simple UDP application.
In win10 when all interfaces are disabled and when I try send udp packet OnTimer twice - then application closes.

If I send packet only once then I see exception error window - socket error #10051 and buttons OK/abort.
BUT: if packet sends during this window shown then application automatically closes (or crashes).

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2.   var s :string;
  3. begin
  4.  
  5.   s:='teststr';
  6.   if flag >0 then begin
  7.     flag:= flag-1;
  8.     IdUDPServer1.Send('192.168.0.100', 12345, s);
  9.   end;
  10.  
  11. end;

When flag=1 (once send)  then I see error window.
When flag=2   then:
   a)  I see error window, wait 2 sec (timer interval), then application closes
   b)  I see error window, now I CLICK OK, then I see new error window aftter 2 sec and application works OK

                         

« Last Edit: December 26, 2024, 07:09:42 am by vladbfg »

DragoRosso

  • New Member
  • *
  • Posts: 42
Re: Indy UDP crashes the application
« Reply #1 on: December 26, 2024, 11:37:06 am »
I tried with Delphi and two windows with the error message are correctly displayed, without the program crashing.

I think the crash is due to the way Lazarus displays the windows in response to an exception.

You should in any case do a try ... except in the calls where errors could occur, it is always good practice to intercept such situations and not leave the management to the OS:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2.   var s :string;
  3. begin
  4.  
  5.   s:='teststr';
  6.   try
  7.     if flag >0 then
  8.       begin
  9.        flag:= flag-1;
  10.        IdUDPServer1.Send('192.168.212.100', 12345, s);
  11.       end;
  12.   except on e:exception do
  13.      ; ////// do something if you need it
  14.   end;
  15. end;

vladbfg

  • New Member
  • *
  • Posts: 16
Re: Indy UDP crashes the application
« Reply #2 on: December 26, 2024, 12:17:52 pm »
I tried with Delphi and two windows with the error message are correctly displayed, without the program crashing.
I think the crash is due to the way Lazarus displays the windows in response to an exception.
You should in any case do a try ... except in the calls where errors could occur, it is always good practice to intercept such situations and not leave the management to the OS:
Thx, yes, in Delphi all OK (that project I ported from Delphi).
I think that in Delphi ONtimer stopped when exception is occured. Timer in delphi seems one-task: for example, it stops when I call MessageDlg() in timer and wait when user click OK/No in the dialog, then new event ONTimer is called(may be from queue).
But in Lazarus timer seems multi-tread, it call ONtimer event regardless of code in ONTimer().  It not wait code execution in ONTimer(), it call it again and again.

May be it is obviously for freepascal/laz users,  may be I'm used to it this way and long time used Delphi7  :)

« Last Edit: December 26, 2024, 12:21:56 pm by vladbfg »

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: Indy UDP crashes the application
« Reply #3 on: December 26, 2024, 12:27:37 pm »
Hi
Mmmm, I've learnt, that in Lazarus, you have to make your 'OnTimer' code reentrant capable.
Maybe you can disable timer in the beginning of your 'OnTimer' method and the re-enable the timer, when you're done?!?
Regards Benny

PS.: As @MarkMLl can attest to, critical sections are no good within the same thread!
« Last Edit: December 26, 2024, 12:29:44 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

DragoRosso

  • New Member
  • *
  • Posts: 42
Re: Indy UDP crashes the application
« Reply #4 on: December 26, 2024, 02:05:24 pm »
Thx, yes, in Delphi all OK (that project I ported from Delphi).
I think that in Delphi ONtimer stopped when exception is occured. Timer in delphi seems one-task: for example, it stops when I call MessageDlg() in timer and wait when user click OK/No in the dialog, then new event ONTimer is called(may be from queue).
But in Lazarus timer seems multi-tread, it call ONtimer event regardless of code in ONTimer().  It not wait code execution in ONTimer(), it call it again and again.
May be it is obviously for freepascal/laz users,  may be I'm used to it this way and long time used Delphi7  :)

Timer events are handled differently than what normally happens for "events".
A timer event occurs even if the previous event has not ended. This is the case in both Delphi and Lazarus.
And in Delphi, as I mentioned, two windows are shown at the same time (delayed by 1 second in the display obviously).

Mmmm, I've learnt, that in Lazarus, you have to make your 'OnTimer' code reentrant capable.
Maybe you can disable timer in the beginning of your 'OnTimer' method and the re-enable the timer, when you're done?!?
PS.: As @MarkMLl can attest to, critical sections are no good within the same thread!

If you need to call timer event only ONE time simply disable it on entering and reenable on exit:

N:B.: PLEASE NOTE THAT THIS ONLY WORKS IF YOU USE TRY / EXCEPT OR TRY / FINALLY

Code: Pascal  [Select][+][-]
  1.     procedure TForm1.Timer1Timer(Sender: TObject);
  2.       var s :string;
  3.     begin
  4.       Timer1.Enabled := false;
  5.       s:='teststr';
  6.       try
  7.         if flag >0 then
  8.           begin
  9.            flag:= flag-1;
  10.            IdUDPServer1.Send('192.168.212.100', 12345, s);
  11.           end;
  12.       except on e:exception do
  13.          ; ////// do something if you need it
  14.       end;
  15.     Timer1.Enabled := True;
  16.     end;

 

TinyPortal © 2005-2018