问题
Im trying to process a plain text file (loaded into a StringList), by using class Tbb2uc but getting AV at calling function GetAddress.
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;
The execution does not even jump into the function. I get the AV at calling this function.
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;
Using the above functions:
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;
The execution is ok until calling GetAddress. At debugging, when evaluate SlInput[i+2], i get 'Expression illegal in evaluator' and right now i have no idea. As you see im only processing the lines of the stringlist.
回答1:
You are accessing your stringlist out of bounds.
for i := 0 to SlInput.Count -1 do begin
// ...(etc)
addr := GetAddress(SlInput[i+2]);
i
is running to the full size of the list. You are indexing two higher than that.
回答2:
It was a static array that i was over-indexing. The debugger cannot pinpoint the exact line so it was not obvious (for me):
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;
Sorry... indeed my question was far from complete.
来源:https://stackoverflow.com/questions/31534594/expression-illegal-in-evaluator-access-violation