Advertisement
WarPie90

Lape array bug trigger | Solved

Jun 25th, 2023 (edited)
1,380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.24 KB | None | 0 0
  1. program new;
  2. (*
  3.   There was no bug, we used move on an array without cleanup old reference.
  4. *)
  5.  
  6. type
  7.   TPathStruct = packed record pts: TPointArray; d: Double; end;
  8.   TQueueHelper = record
  9.     lo, hi: Int32;
  10.     data: array of TPathStruct;
  11.   end;
  12.  
  13. procedure TQueueHelper.Init(sz: Int32);
  14. begin
  15.   SetLength(Self.data, sz);
  16.  
  17.   Self.hi := -1;
  18.   Self.lo := 0;
  19. end;
  20.  
  21. function TQueueHelper.SearchClosest(value: Double): Int32;
  22. var
  23.   l, h: Int32;
  24. begin
  25.   l := Self.Lo;
  26.   h := Self.Hi;
  27.  
  28.   while l <= h do
  29.   begin
  30.     Result := (l + h) div 2;
  31.     if Self.Data[Result].d < value then
  32.       h := Result - 1
  33.     else if Self.Data[Result].d > value then
  34.       l := Result + 1
  35.     else
  36.       Exit(Result);
  37.   end;
  38. end;
  39.  
  40. function TQueueHelper.Insert(path:TPointArray; distance: Double): Int32;
  41. var
  42.   idx: Int32;
  43. begin
  44.   idx := Self.SearchClosest(distance);
  45.   if Self.Data[idx].d > distance then Inc(idx);
  46.  
  47.   if (self.hi+1-idx) > 0 then
  48.   begin
  49.     WriteLn('>>> move from: ', idx,' to ', idx+1,', bytes: ', (self.hi+1-idx)*(SizeOf(TPathStruct)));
  50.     Move(Self.data[idx], Self.data[idx+1], (self.hi+1-idx)*(SizeOf(TPathStruct)));
  51.  
  52.     // -- EDIT 2025: ----------
  53.     Move(blank, Self.data[idx], SizeOf(TPathStruct)); // it was this or update refcount  
  54.  
  55.     // problem was that we duplicated an array without refcount update
  56.     // ------------------------
  57.   end;
  58.  
  59.   Self.data[idx].d   := distance;
  60.   Self.data[idx].pts := path;
  61.  
  62.   Inc(Self.hi);
  63. end;
  64.  
  65.  
  66. procedure TQueueHelper.Pop(out path:TPointArray; out distance: Double);
  67. begin
  68.   path     := self.data[self.hi].pts;
  69.   distance := self.data[self.hi].d;
  70.   Dec(Self.Hi);
  71. end;
  72.  
  73. function TQueueHelper.AsString(): String;
  74. var
  75.   tmp: array of TPathStruct;
  76. begin
  77.   tmp := self.data;
  78.   SetLength(tmp, self.hi+1);
  79.   Result := ToString(tmp);
  80. end;
  81.  
  82.  
  83.  
  84. var
  85.   q: TQueueHelper;
  86.   p: tPointArray;
  87.   d: Double;
  88. begin
  89.   q.Init(1000);
  90.  
  91.   q.Insert([[2,2]], 2);
  92.   WriteLn q.AsString;
  93.  
  94.   q.Insert([[2,1], [2,2]], 4);
  95.   WriteLn q.AsString;
  96.  
  97.   q.Insert([[2,2], [1,2]], 8);
  98.   WriteLn q.AsString;
  99.  
  100.   q.Insert([[2,2], [3,2]], 16);
  101.   WriteLn q.AsString;
  102.  
  103.   q.Insert([[2,2], [2,3]], 32);
  104.   WriteLn q.AsString;
  105.  
  106.   q.Insert([[2,2], [2,3], [4,4]], 64);
  107.   WriteLn q.AsString;
  108. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement