[Solved] Strange error on loop lazarus / 21-General ---------------------------------------------------------------------- nikel · 2023-11-14 13:49 Hello, I'm trying to detect if two strings contain some string. Here's my code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TForm1.IsEmptyDirectory(Index: Integer): Boolean;var  I          : Integer;  J          : Integer;  List       : TStringList; procedure IsEmptyDirectoryUnset;begin  I:=0;  J:=0;  List.Free;  Exit;end;begin  I:=0;  J:=0;   List:=TStringList.Create;  List.Sorted:=False;  List.LoadFromFile(Path);   if (Index >= List.Count - 1) or (List[Index] = '') then  begin    Result:=False;    IsEmptyDirectoryUnset;  end;     while I <= List[Index].Length - 4 do  begin    if ((List[Index].SubString(I, 4) = '+---') or (List[Index].SubString(I, 4) = '\---')) then    begin      J:=0;     while (J <= List[Index + 1].Length - 4) do      begin        if (List[Index + 1].SubString(J, 4) = '+---') or (List[Index + 1].SubString(J, 4) = '\---') then        begin           Result:=True;          IsEmptyDirectoryUnset;          Break;        Exit;        end;         J:=J + 4;      end;    end;     I:=I + 4;  end;   Result:=False;   IsEmptyDirectoryUnset;end; This is giving me an error. I attached a screenshot. Can anyone help me? ---------------------------------------------------------------------- Zvoni · 2023-11-14 13:54 Line 20: Where is "Path" coming from? Line 12: The "Exit" jumps out of the (nested) procedure, not the function. So, basically, if you pass an invalid index, your code continues on, and tries to access an invalid Index of List. It's the reason, why i use (nested) Functions instead of procedures for unexpected cleanup Aircode Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function IsEmptyDirectoryUnset:Boolean;begin  I:=0;  J:=0;  List.Free;  Result:=True;end;....//For your Lines 22 to 26if (Index >= List.Count - 1) or (List[Index] = '') then     If IsEmptyDirectoryUnset Then Exit(False);   Line 43 + 44: Break followed by Exit?? huh? With Break you jump out of the Loop.... Exit gets never executed.... ---------------------------------------------------------------------- Fibonacci · 2023-11-14 14:02 On line 43 you break the loop ("exit" is unrechable), and the loop from line 30 continues On line 41 you set result to true, break the loop, but again loop from line 30 continues, and after it finishes result will be set to false on line 54 This whole code looks broken ---------------------------------------------------------------------- rvk · 2023-11-14 14:02 On one hand do you check on line 22 if Index is out of range of List.Count. But on the other hand... if that's the case you STAY in that procedure and the line 30 List[Index].Lengte will crash because Index is out of range of the List array. So put an exit after IsEmptyDirectoryUnset; Second... you call Exit on line 12 inside the procedure IsEmptyDirectoryUnset, but that doesn't do anything. It will exit the current procedure, NOT the procedure that called it (see remark above). About the List.Free; in IsEmptyDirectoryUnset, it might be better that you use try/finally construct. This is much better than calling IsEmptyDirectoryUnset (because I see you also call it multiple times). So remove that subdirectory and create a propper try/finally structure. ---------------------------------------------------------------------- nikel · 2023-11-14 14:14 Thanks for the replies. Zvoni, Fibonacci and rvk. I put "Exit" after unset and worked fine. Also I'm planning to use "try / finally". ---------------------------------------------------------------------- rvk · 2023-11-14 14:59 Without actually knowing what it does... you could restructure your code like this: (with the try/finally, even with exit, the finally part is always executed) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TForm1.IsEmptyDirectory(Idx: integer): boolean;var  I: integer;  J: integer;  List: TStringList;begin  Result := False; // Default result  I := 0;  J := 0;   List := TStringList.Create;  try    List.Sorted := False;    List.LoadFromFile(Path);     if (Idx >= List.Count - 1) or (List[Idx] = '') then      Exit(False); // Result := False and exit   while I <= List[Idx].Length - 4 do    begin      if ((List[Idx].SubString(I, 4) = '+---') or (List[Idx].SubString(I, 4) = '\---')) then      begin        J := 0;         while (J <= List[Idx + 1].Length - 4) do        begin          if (List[Idx + 1].SubString(J, 4) = '+---') or (List[Idx + 1].SubString(J, 4) = '\---') then          Exit(True); // same as Result := true and exit          J := J + 4;        end;      end;       I := I + 4;    end;   finally    List.Free;  end; end;