Recent

Author Topic: Random crash in iterator  (Read 3118 times)

Nitorami

  • Hero Member
  • *****
  • Posts: 505
Random crash in iterator
« on: April 12, 2024, 02:02:56 pm »
What's going on here ?

Code: Pascal  [Select][+][-]
  1. program mist4;
  2.  
  3. var TiF: double;
  4.  
  5. begin
  6.   for Tif in [0.1, 0.2, 0.5, 1.0, 10.0] do begin
  7.     writeln (TiF);
  8.   end;
  9. end.
  10.  

Output:

Running "c:\temp\mist4.exe 7"
 1.0000000000000001E-001
 2.0000000000000001E-001
 0.0000000000000000E+000
Runtime error 205 at $0040164C
  $0040164C  main,  line 5 of C:/Users/e09neum0/Documents/PAS/GX1/mist4.pas
  $0040A727


Sometimes runtime error 205, sometimes 207, sometimes division by zero or other. Depends on the numbers given.  Not sure whether I can use floating point values here, but if not, the compiler should throw an error.

FPC 3.3.2, i386-win32.

Fibonacci

  • Sr. Member
  • ****
  • Posts: 447
  • Internal Error Hunter
Re: Random crash in iterator
« Reply #1 on: April 12, 2024, 02:20:04 pm »

Eugene Loza

  • Hero Member
  • *****
  • Posts: 702
    • My games in Pascal
Re: Random crash in iterator
« Reply #2 on: April 12, 2024, 02:21:30 pm »
Works for me without any issues
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10005
  • Debugger - SynEdit - and more
    • wiki
Re: Random crash in iterator
« Reply #3 on: April 12, 2024, 02:22:22 pm »
It seems 32 bit only...

IIRC there is an issue that if you have an inlined list/array, the type is determined by the FIRST element ONLY. And all others are truncated or otherwise handled faulty.
E.g. a list of shortstring, will take the length of the first string, and cut all others.

I don't know, if your test hits this issue.

Nitorami

  • Hero Member
  • *****
  • Posts: 505
Re: Random crash in iterator
« Reply #4 on: April 12, 2024, 02:30:51 pm »
@Fibonacci: Sorry, typo. I meant 3.2.2.

@martin_fr: ok but all elements are double and of same size, so I can't see the problem ?

YiannisKam

  • Full Member
  • ***
  • Posts: 106
Re: Random crash in iterator
« Reply #5 on: April 12, 2024, 02:40:00 pm »
There is something wrong here (FPC 3.2.2, Windows 10 64bit)
Code: Pascal  [Select][+][-]
  1. program ForIn;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. var
  6.   d: double;
  7.  
  8. begin
  9.   for d in [0.1, 0.2, 0.5, 1.0, 10.0] do
  10.     WriteLn(d);
  11.   ReadLn;
  12. end.
Output:
1.0000000000000001E-001
 2.0000000000000001E-001
 5.2220990168285998E-315
 5.2635442471208903E-315
 5.3982412455708344E-315

And if:
Code: Pascal  [Select][+][-]
  1. program ForIn;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. var
  6.   d: double;
  7.  
  8. begin
  9.   for d in [0.1, 0.2, 0.5, 1.0, 10.0] do
  10.     WriteLn(d:0:1);
  11.   ReadLn;
  12. end.
Output:
0.1
0.2
0.0
0.0
0.0

Nitorami

  • Hero Member
  • *****
  • Posts: 505
Re: Random crash in iterator
« Reply #6 on: April 12, 2024, 02:54:06 pm »
THIS works  :P
So it has to do with the size of elements....

Code: Pascal  [Select][+][-]
  1. program mist4;
  2. var TiF: double;
  3.  
  4. begin
  5.   for Tif in [double(0.1), double(0.2), double(0.5), double(1.0), double(10.0)] do begin
  6.     writeln (TiF);
  7.   end;
  8. end.

Running "c:\temp\mist4.exe 7"
 1.0000000000000001E-001
 2.0000000000000001E-001
 5.0000000000000000E-001
 1.0000000000000000E+000
 1.0000000000000000E+001



Thaddy

  • Hero Member
  • *****
  • Posts: 14825
  • Censorship about opinions does not belong here.
Re: Random crash in iterator
« Reply #7 on: April 12, 2024, 05:38:47 pm »
Code: Bash  [Select][+][-]
  1. 0.1000
  2. 0.2000
  3. 0.5000
  4. 1.0000
  5. 10.0000
based on
Code: Pascal  [Select][+][-]
  1. var TiF: double;
  2. begin
  3.   for Tif in [0.1, 0.2, 0.5, 1.0, 10.0] do
  4.     writeln (TiF:4:4);
  5. end.
On Windows11/64 trunk 3.3.1 from this week.
« Last Edit: April 12, 2024, 05:40:47 pm by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Nitorami

  • Hero Member
  • *****
  • Posts: 505
Re: Random crash in iterator
« Reply #8 on: April 12, 2024, 05:47:30 pm »
Please try something like this
Code: Pascal  [Select][+][-]
  1. for Tif in [-3, 0.2, 0.5, 1.0, 10.0]

I don't have trunk but it fails in Lazarus 2.2.6 (x64). It seems the first element is parsed as integer and than the rest goes wrong.

-3.0000000000000000E+000
-1.0200000000000000E+002
 0.0000000000000000E+000
 0.0000000000000000E+000
 0.0000000000000000E+000

Bart

  • Hero Member
  • *****
  • Posts: 5306
    • Bart en Mariska's Webstek
Re: Random crash in iterator
« Reply #9 on: April 12, 2024, 06:05:43 pm »
Please try something like this
Code: Pascal  [Select][+][-]
  1. for Tif in [-3, 0.2, 0.5, 1.0, 10.0]
Fixed in fpc main.
Now all that rests is to test with fpc 3.2 fixes branch to see wether this was merged.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14825
  • Censorship about opinions does not belong here.
Re: Random crash in iterator
« Reply #10 on: April 12, 2024, 06:16:03 pm »
I don't have trunk but it fails in Lazarus 2.2.6 (x64). It seems the first element is parsed as integer and than the rest goes wrong.
It is not related to Lazarus, but to FPC and it is fixed in trunk, nowadays called main for some reason.(FPC trunk, not Lazarus trunk)
Such issues should always refer to the compiler version, not a Lazarus version.
« Last Edit: April 12, 2024, 06:18:14 pm by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

VisualLab

  • Sr. Member
  • ****
  • Posts: 359
Re: Random crash in iterator
« Reply #11 on: April 12, 2024, 11:04:43 pm »
Test in Windows 10 64-bit, Lazarus 3.0 64-bit (FPC 3.2.2):

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.   {TForm1}
  12.   TForm1 = class(TForm)
  13.     Button1: TButton;
  14.     Memo1: TMemo;
  15.     procedure Button1Click(Sender: TObject);
  16.   end;
  17.  
  18.   TNumberArray = array [1..5] of Double;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. {TForm1}
  28.  
  29. procedure TForm1.Button1Click(Sender: TObject);
  30. const
  31.   ConstNumbers: array [1..5] of Double = (0.1, 0.2, 0.5, 1.0, 10.0);
  32. var
  33.   TiF: Double;
  34.   VarNumbers: TNumberArray;
  35. begin
  36.   Memo1.Lines.Clear;
  37.   {numbers as literals}
  38.   Memo1.Lines.Add('numbers as literals:');
  39.   for Tif in [0.1, 0.2, 0.5, 1.0, 10.0] do
  40.    begin
  41.     Memo1.Lines.Add(Tif.ToString);
  42.    end;
  43.   Memo1.Lines.Add('---');
  44.   {numbers in constant}
  45.   Memo1.Lines.Add('numbers in constant:');
  46.   for Tif in ConstNumbers do
  47.    begin
  48.     Memo1.Lines.Add(Tif.ToString);
  49.    end;
  50.   Memo1.Lines.Add('---');
  51.   {numbers in variable}
  52.   Memo1.Lines.Add('numbers in variable:');
  53.   VarNumbers[1] := 0.1;
  54.   VarNumbers[2] := 0.2;
  55.   VarNumbers[3] := 0.5;
  56.   VarNumbers[4] := 1.0;
  57.   VarNumbers[5] := 10.0;
  58.   for Tif in VarNumbers do
  59.    begin
  60.     Memo1.Lines.Add(Tif.ToString);
  61.    end;
  62. end;
  63.  
  64. end.

Results in attachment (screenshot).

paweld

  • Hero Member
  • *****
  • Posts: 1060
Re: Random crash in iterator
« Reply #12 on: April 13, 2024, 08:11:07 am »
@VisualLab: Windows 10 64-bit, Lazarus trunk + FPC 3.2-fixes (32 and 64-bit)
Code: [Select]
numbers as literals:
0,1
0,2
0,5
1
10
---
numbers in constant:
0,1
0,2
0,5
1
10
---
numbers in variable:
0,1
0,2
0,5
1
10
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018