Search code examples
lazarusfreepascal

Lazarus error "External: SIGSEGV" on variable increment?


I got a problem in my Lazarus project: everytime I want to use a function it throws the above error (External: SIGSEGV). I don't know what that means, but some debugging showed me, that this is the code, causing the error:

class function TUtils.AsStringArray(const Strs:TStrings): TStringArray;
var
  s:string;
  i:integer;
begin
  SetLength(Result, Strs.Count);
  i := 1;
  for s in Strs do
  begin
    Result[i] := s;
    i := i + 1;
  end;
end; 

And the definitions

TStringArray = array of string; 

TUtils = class
  public
    [...]
    class function AsStringArray(const Strs:TStrings): TStringArray; static;
end;   

The exception occurs after i := i + 1;. I would be really thankful if you could help me!


Solution

  • Dynamic arrays such as TStringArray = array of string; are zero-based; your code uses it as 1-based and raises access violation.

    You should replace i := 1; by i := 0;