TKKK = class(TObject)
private
FK: Integer;
procedure SetK(const Value: Integer);
public
property K: Integer read FK write SetK;
end; TForm1 = class(TForm)
Button4: TButton;
procedure Button4Click(Sender: TObject);
private
F1, F2: TKKK;
public end;implementation{$R *.dfm}procedure TKKK.SetK(const Value: Integer);
begin
FK := Value;
end;procedure TForm1.Button4Click(Sender: TObject);
begin
F1 := TKKK.Create;
F2 := F1;
F1.Free;
F1 := nil;
if 如何判断F2现在已经释放 then
F2.K := 10;
end;
private
FK: Integer;
procedure SetK(const Value: Integer);
public
property K: Integer read FK write SetK;
end; TForm1 = class(TForm)
Button4: TButton;
procedure Button4Click(Sender: TObject);
private
F1, F2: TKKK;
public end;implementation{$R *.dfm}procedure TKKK.SetK(const Value: Integer);
begin
FK := Value;
end;procedure TForm1.Button4Click(Sender: TObject);
begin
F1 := TKKK.Create;
F2 := F1;
F1.Free;
F1 := nil;
if 如何判断F2现在已经释放 then
F2.K := 10;
end;
因为f1,f2都是指针,并且指向相同地址,f1对应的对象内存free了,f2指向的就是那个对象死尸!当然不能用!如果你的测试代码改一下,那么检测一个object是否free过就try一下调用此object的函数,有异常就很可能是free过了,但不一定异常不是合法函数内部触发的。没啥好办法。
是的,有异常就很可能是free过了,但不一定异常不是合法函数内部触发的。
所以还是没啥好办法。
如果有办法将所有引用过该对象的指针都赋空也不错呀。
对象赋值的代价。