【问题标题】:how to detect if tfilestream has been freed?如何检测 tfilestream 是否已被释放?
【发布时间】:2010-07-27 16:48:21
【问题描述】:

有没有办法查看是否正在使用 tfile 流的实例? 例如,如果我声明 tfilestream 类型的 FS,将缓冲区写入它并 最后使用 tfilestream.free 释放流我可以检查一下吗 喜欢:

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

activebeingused 方法并不真实存在,我们也不能测试 tfilestream.free = true 只是为了说明什么我想问

【问题讨论】:

  • 没有办法检测一个对象是否被释放,因为内存可以被其他对象重用。正确的做法是在对象引用被释放后基本上不重用它们,当你确实需要时,使用 FreeAndNil,就像 Robert 指出的那样。

标签: delphi filestream tfilestream


【解决方案1】:

你不能按照你期望的方式去做。但是你用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;

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 2021-11-12
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多