jpfollenius
jpfollenius

Reputation: 16612

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.

Upvotes: 3

Views: 381

Answers (1)

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58441

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

Upvotes: 5

Related Questions