Search code examples
delphifilestreamtfilestream

how to detect if tfilestream has been freed?


is there a way to see if an instace of tfile stream is being used? for example if i declare FS of type tfilestream,write buffer to it and finally free the stream using tfilestream.free can i check something like:

if 
tfilestream.NotActive
then
 //code
if tfilestream.beingused then
 //code
if tfilestream.free = true then
    //code

active and beingused methods do not exists for real nor can we test tfilestream.free = true just making this up to give idea what i am trying to ask


Solution

  • You can't do it in the way you expect. But you and do it with FreeAndNil()

    var
      FS : TFileStream;
    begin
      FS := TFileStream.Create(...);
      try
       // Do Work with TFileSTream 
      finally 
       FreeAndNil(FS);
      end;
    
      // For some reason you need to check FS is freed.
    
      if not Assigned(FS) then
      begin
       // Stream was freed.
      end;
    end;