以往的Delphi版本,不支持接口的Weak,和UnSafe的引用,支持对象的Weak, UnSafe,并且仅在Android和Ios平台上支持。spa
如今Delphi XE10.1 Berlin终于增长了对接口的Weak, UnSafe的支持。orm
1.Weak对象
Weak引用,不影响引用计数器,可是若是对象被释放,Weak引用变量自动清0,来看例子:blog
type TA=class(TInterfacedObject) end; procedure TForm1.Button1Click(Sender: TObject); var a:IInterface; [weak]aweak:IInterface; begin a:=TA.Create; //建立对象,复制给a,执行完成后引用计数器=1 aweak:=a; //因为aweak定义有[weak]属性,因此赋值给aweak后,引用计数器依旧为1,但aweak变量的地址被保存到一个weak关联列表中 Memo1.Lines.Add(Format('Ptr:%d', [NativeInt(Pointer(aweak))])); a:=nil; //因为引用计数器=1,执行此句后,计数器清0,对象被释放,同时与此对weak关联列表中全部变量也被赋值为nil,包括aweak变量. Memo1.Lines.Add(Format('Ptr:%d', [NativeInt(Pointer(aweak))])); end;
运行结果接口
Ptr:16360080 Ptr:0
weak引用很是适合用于两个对象须要互相引用的状况下,若是以往的引用,将没法让引用计数器清0.内存
以下面的例子,互相引用后,两个对象的计数器都不清0,致使内存泄漏class
type ISimpleInterface = interface procedure DoSomething; procedure AddObjectRef (simple: ISimpleInterface); end; TObjectOne = class (TInterfacedObject, ISimpleInterface) private anotherObj: ISimpleInterface; public procedure DoSomething; procedure AddObjectRef (simple: ISimpleInterface); end; ..................... procedure TObjectOne.AddObjectRef (simple: ISimpleInterface); begin anotherObj:=simple; end; ..................... var one, two: ISimpleInterface; begin one := TObjectOne.Create; two := TObjectOne.Create; one.AddObjectRef (two); two.AddObjectRef (one);
这时候在Delphi XE10.1 Berlin下能够用weak引用,来快速方便的解决泄漏问题:变量
private [weak] anotherObj: ISimpleInterface;
2.UnSafe内存泄漏
unsafe引用,不影响引用计数,但不会向Weak引用那样清零引用的变量。引用
type TA=class(TInterfacedObject) end; procedure TForm1.Button2Click(Sender: TObject); var a:IInterface; [unsafe]aunsafe:IInterface; begin a:=TA.Create; aunsafe:=a; Memo1.Lines.Add(Format('Ptr:%d', [NativeInt(Pointer(aunsafe))])); a:=nil; Memo1.Lines.Add(Format('Ptr:%d', [NativeInt(Pointer(aunsafe))])); end;
运行结果
Ptr:42640064 Ptr:42640064
因为Unsafe引用,不影响应用计数器,下面的程序将致使内存泄漏:
procedure TForm1.Button2Click(Sender: TObject); var [unsafe] one: ISomeInterface; begin one := TSomeObject.Create; one.DoSomething; end;