Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception when destroying TReader

The following code throws an EZDecompressionError with message 'Invalid ZStream operation' whenever the line

Reader.Free

is executed. Can someone tell me what's wrong with this code?

Reader := nil;
Decompressor := nil;
InputFile := TFileStream (FileName, fmOpenRead);
try
  Decompressor := TDecompressionStream.Create (InputFile);
  Reader := TReader.Create (Decompressor, 1024);
  SomeString := Reader.ReadString;
finally
  Reader.Free
  Decompressor.Free;
  InputFile.Free;
end;

I tested to change the order of the memory freeing commands but that doesn't seem to help. Leaving out the Reader.Free line of course results in a memory leak.

like image 871
jpfollenius Avatar asked Mar 14 '26 12:03

jpfollenius


1 Answers

Smasher

TReader does a FStream.Seek(FBufPos - FBufCount, soCurrent) in its destructor.

The error get's raised because of a backwards seek. If you call Reader.FlushBuffer and Reader.Position := soFromBeginning before freeing the reader, does the error disappear?


From the comments of TDecompressionstream. TDecompressionStream is read-only and unidirectional; you can seek forward in the stream, but not backwards.

Regards,
Lieven

like image 60
Lieven Keersmaekers Avatar answered Mar 16 '26 11:03

Lieven Keersmaekers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!