how to detect if tfilestream has been freed?

 ̄綄美尐妖づ 提交于 2019-12-11 09:58:15

问题


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


回答1:


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;


来源:https://stackoverflow.com/questions/3345905/how-to-detect-if-tfilestream-has-been-freed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!