the violation of the delphi access agreement created tfilestream closed

The access violation is caused by the wrong way to create a TFileStream instance, resulting is fs variable having an invalid value. As with any other object, you have to call the class constructor and assign the result to the variable: fs := TFileStreaM.create('aaa.ini', fmOpenRead);.

I have revised your code:

var
  sr : TStreamReader;
  fs : TFileStream;
begin
   try
     fs := TFileStream.Create('aaa.ini', fmOpenRead);
     try
       sr := tStreamReader.Create(fs, TEncoding.UTF8);
       try
         connAtomo.Close;
         connAtomo.Params.Clear;
         while not sr.EndOfStream do
           connAtomo.Params.Add(sr.ReadLine);
       finally
         sr.Free;
       end;
     finally
       fs.Free
     end;
   except
      on E: EFileStreamError do
         ShowMessage('File error ' + E.Message);
   end;
end;

As you can see, I have used try/finally to protect the allocated objects and remove the with usage which is not recommended.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top