Pascal Segmentation Fault 解析文本文件

Pascal Segmentation Fault parsing Text File

我正在使用 Pascal / Lazarus 开发 Question/Answer UI 应用程序。我的问题是,通过单击按钮调用以下代码后,程序崩溃并出现 Segmentation Fault 错误。

// more declarations... (UI Form, Buttons, ...)

type
  TQuestion = class(TObject)
    title: string;
    answers: array of string;
    correct: integer;
  end;

var
  questions: array of TQuestion;

procedure TForm1.BStartClick(Sender: TObject);
var
  i: integer;
  j: integer;
  line: string;
  arrayLength: integer;
  question: TQuestion;
  stringList: TStringList;
begin
  stringList := TStringList.create;
  stringList.LoadFromFile('questions.txt');

  for i := 0 to stringList.Count - 1 do ;
  begin
    line := stringList[i];
    if (length(line) >= 2) then
      if (line[2] = ' ') and ((line[1] = '-') or (line[1] = '+')) then
      begin
        arrayLength := length(question.answers);
        SetLength(question.answers, arrayLength + 1);
        question.answers[arrayLength] :=
          Copy(line, 2, Length(line) - 1);

        if zeile[1] = '+' then
          question.correct := arrayLength;
      end
      else
      begin
        question := TQuestion.Create;
        question.title := line;

        arrayLength := length(questions);
        setLength(questions, arrayLength + 1);
        questions[arrayLength] := question;
      end;
  end;
  BStart.Visible := False;
end;

好吧,我的 Pascal 知识还停留在 10 到 15 年前。但是,我可以看到您在这一行的末尾多了一个分号:

for i := 0 to stringList.Count - 1 do ;