评估者中的表达非法(& Access Violation)

时间:2015-07-21 08:59:24

标签: delphi delphi-2010 access-violation

我试图通过使用类Tbb2uc处理纯文本文件(加载到StringList中),但在调用函数GetAddress时获取AV。

  TArrayQuotePositions = array[1..4] of integer;

  Tbb2uc = class(TObject)
  private
    Farrayquotes: TArrayQuotePositions;
    SlInput: TStringList;
    Inputfilename: TFileName;
    SlOutput: TStringList;
    function GetQuotePositions( aLine: string ): TArrayQuotePositions;
    function GetInvoice( aLine: string ): string;
    function GetName( aLine: string ): string;
    function GetAddress( aLine: string ): string;
    function GetSwift( aLine: string ): string;
    function ProcessSl: integer;        
  public
    constructor Create;
    destructor Destroy; override;
    function OpenFile: integer;
  end;
function Tbb2uc.GetInvoice( aLine: string ): string;
var
  quotesPos: TArrayQuotePositions;
begin
  quotesPos := GetQuotePositions( aLine );
  result := copy( aLine, quotesPos[3]+1, (quotesPos[4]-quotesPos[3])-1 );
end;
function Tbb2uc.GetName( aLine: string ): string;  
var
  quotesPos: TArrayQuotePositions;
begin
  quotesPos := GetQuotePositions( aLine );
  result := copy( aLine, quotesPos[3]+1, (quotesPos[4]-quotesPos[3])-1 );
end;

执行甚至没有跳转到该功能。我在调用此功能时获得了AV。

function Tbb2uc.GetAddress( aLine: string ): string;
var
  quotesPos: TArrayQuotePositions;
  address1: string;
  address2: string;
begin
  quotesPos := GetQuotePositions( aLine );
  address1 := copy( aLine, quotesPos[1]+1, (quotesPos[2]-quotesPos[1])-1 );
  address2 := copy( aLine, quotesPos[3]+1, (quotesPos[4]-quotesPos[3])-1 );
  result := address1 + ' ' + address2;
end;

使用上述功能:

function Tbb2uc.ProcessSl: integer;
var
  i: integer;
  line: string;
  invoice,name,addr,swift: string;
begin
  SlInput.LoadFromFile( string(Inputfilename) );
  //
  for i := 0 to SlInput.Count -1 do begin
    if ansipos( STARTSTRING, SlInput[i]) <> 0 then begin
      invoice := GetInvoice(SlInput[i]);
      name := GetName(SlInput[i+1]);
      ////
      addr := GetAddress(SlInput[i+2]); //Access Violation
      ////
      swift := GetSwift(SlInput[i+4]);
      line := line + invoice + ';' + name + ';' + addr + ';' + swift;
    end;
    SlOutput.Add(line);
    line := '';
  end;
  SlOutput.SaveToFile( OUTPUT_FNAME );
end;

在调用GetAddress之前执行正常。在调试时,当评估SlInput [i + 2]时,我得到'表达式在评估器中非法',现在我不知道。如你所见,我只处理字符串列表的行。

2 个答案:

答案 0 :(得分:3)

您正在访问您的字符串列表。

for i := 0 to SlInput.Count -1 do begin

 // ...(etc)

      addr := GetAddress(SlInput[i+2]); 

i正在运行到列表的完整大小。你索引两个高于那个。

答案 1 :(得分:-1)

这是一个静态数组,我过度索引。调试器无法精确定位确切的行,因此不明显(对我来说):

function Tbb2uc.GetQuotePositions( aLine: string ): TArrayQuotePositions;
var
  i: integer;
  arraypos: integer;
begin
  for i := low(TArrayQuotePositions) to high(TArrayQuotePositions) do begin
    Farrayquotes[i] := 0;
  end;
  arraypos := low(TArrayQuotePositions);
  for i := 1 to length(aLine) do begin
    if aLine[i] = CHAR_FOR_COLLECT then begin         
      Farrayquotes[arraypos] := i;  //over-indexing, Farrayquotes: TArrayQuotePositions = array[1..4] of integer;
      inc(arraypos);            
    end;
  end;
  result := Farrayquotes;
end;

对不起......我的问题确实还远未完成。