I am totally perplexed by this bug. It appears to be a bug in Delphi.
I was wondering if anyone has some kind of workaround for this. The
following is my code and I have marked the line that causes the access
violation:
try
AssignFile(FromFile, src);
AssignFile(ToFile, dest);
Reset(FromFile);
Rewrite(ToFile);
try
// Make sure file is greater than 10 bytes in length
if (m_nTotal > 10) then begin
while not Eof(FromFile) do begin
ReadLn(FromFile, sLine);
// update progress bar
Inc(m_nComplete, Length(sLine) + 2);
Synchronize(UpdateProgress);
// Check for special case Empty Lines
if sLine = '' then WriteLn(ToFile, sLine)
else begin
// Process first column codes
case sLine[1] of
'0':
begin
if (sPrevLine <> '') then WriteLn(ToFile,
TrimRight(sPrevLine));
WriteLn(ToFile, '');
sPrevLine := Copy(sLine, 2, Length(sLine) - 1);
end;
'-':
begin
if (sPrevLine <> '') then WriteLn(ToFile,
TrimRight(sPrevLine));
WriteLn(ToFile, #13#10);
sPrevLine := Copy(sLine, 2, Length(sLine) - 1);
end;
'+':
begin
if (sPrevLine <> '') then begin
sLine := Copy(sLine, 2, Length(sLine) - 1);
sLine := TrimRight(sLine);
_InsertOverwrite(sLine, sPrevLine);
end;
end;
'1':
begin
if (sPrevLine <> '') then WriteLn(ToFile,
TrimRight(sPrevLine));
Write(ToFile, #12);
sPrevLine := Copy(sLine, 2, Length(sLine) - 1);
end;
else
begin
if (sPrevLine <> '') then WriteLn(ToFile,
TrimRight(sPrevLine));
sPrevLine := Copy(sLine, 2, Length(sLine) - 1);
end;
end; {case}
end; {else}
end; {while}
// Write last line to file
if (sPrevLine <> '') then
WriteLn(ToFile, TrimRight(sPrevLine)); // <-- ACCESS
VIOLATION
end
else bFileTooSmall := True;
finally
// Close source and destination files
CloseFile(FromFile);
CloseFile(ToFile);
end; {try}
except
bFileMoved := False;
end; {try}
You will notice that there are several calls to
WriteLn(ToFile, TrimRight(sPrevLine))
in the while loop. sPrevLine is declared in the var section of this
particular function as a String.
Keep in mind that this is running in a separate thread.
Is there any known issues with Delphi Strings and threads?
I am running Delphi6 Enterprise with Update 2, so at the time I'm
writing this, I have the latest greatest from Borland.
Also, I should note that stepping into the offensive line of code
brings me to the TrimRight code, and it died at the call to the Copy
function from the System unit.