How and when are variables referenced in Delphi's anonymous methods captured?

后端 未结 1 1932
独厮守ぢ
独厮守ぢ 2020-12-24 07:17

This was prompted by How to compare TFunc/TProc containing function/procedure of object?, specifically by David\'s comment to Barry\'s question. Since I don\'t have a Blog t

相关标签:
1条回答
  • 2020-12-24 07:58

    When you have a function like the one in the question, where you have an anonymous method accessing a local variable, Delphi appears to create one TInterfacedObject descendant that captures all the stack based variables as it's own public variables. Using Barry's trick to get to the implementing TObject and a bit of RTTI we can see this whole thing in action.

    The magic code behind the implementation probably looks like this:

    // Magic object that holds what would normally be Stack variables and implements
    // anonymous methods.
    type ProcedureThatUsesAnonymousMethods$ActRec = class(TInterfacedObject)
    public
      V: string;
      function AnonMethodImp: string;
    end;
    
    // The procedure with all the magic brought to light
    procedure ProcedureThatUsesAnonymousMethods;
    var MagicInterface: IUnknown;
        F1: TFunc<string>;
        F2: TFunc<string>;
    begin
      MagicInterface := ProcedureThatUsesAnonymousMethods$ActRec.Create;
      try
        F1 := MagicInterface.AnonMethod;
        MagicInterface.V := '1';
        F2 := MagicInterface.SomeOtherAnonMethod;
        MagicInterface.V := '2';
        ShowMessage(F1);
        ShowMessage(F2);
      finally MagicInterface := nil;
      end;
    end;
    

    Of course this code doesn't compile. I'm magic-less :-) But the idea here is that an "Magic" object is created behind the scenes and local variables that are referenced from the anonymous method are transformed in public fields of the magic object. That object is uses as an interface (IUnkown) so it gets reference-counted. Apparently the same object captures all used variables AND defines all the anonymous methods.

    This should answer both "When" and "How".

    Here's the code I used to investigate. Put a TButton on a blank form, this should be the whole unit. When you press the button you'll see the following on screen, in sequence:

    • 000000 (bogus number)
    • 000000 (the same number): This proofs both anonymous methods are actually implemented as methods of the same object!
    • TForm25.Button1Click$ActRec: TInterfacedObject : This shows the object behind the implementation, it's derived from TInterfacedObject
    • OnStack:string: RTTI discovers this field on that object.
    • Self: TForm25: RTTI discovers this field on that object. It's used to get the value of ClasVar
    • FRefCount:Integer - this comes from TInterfacedObject
    • Class Var - result of ShowMessage.
    • On Stack - result of ShowMessage.

    Here's the code:

    unit Unit25;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Rtti;
    
    type
      TForm25 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        ClassVar: string;
      public
      end;
    
    var
      Form25: TForm25;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm25.Button1Click(Sender: TObject);
    var F1: TFunc<string>;
        F2: TFunc<string>;
    
        OnStack: string;
    
        i: IInterface;
        o: TObject;
    
        RC: TRttiContext;
        R: TRttiType;
        RF: TRttiField;
    
    begin
      // This anonymous method references a member field of the TForm class
      F1 := function :string
            begin
              Result := ClassVar;
            end;
    
      i := PUnknown(@F1)^;
      o := i as TObject;
      ShowMessage(IntToStr(Integer(o))); // I'm looking at the pointer to see if it's the same instance as the one for the other Anonymous method
    
      // This anonymous method references a stack variable
      F2 := function :string
            begin
              Result := OnStack;
            end;
    
      i := PUnknown(@F2)^;
      o := i as TObject;
      ShowMessage(IntToStr(Integer(o)));
    
      ShowMessage(o.ClassName + ': ' + o.ClassType.ClassParent.ClassName);
    
      RC.Create;
      try
        R := RC.GetType(o.ClassType);
        for RF in R.GetFields do
          ShowMessage(RF.Name + ':' + RF.FieldType.Name);
      finally RC.Free;
      end;
    
      ClassVar := 'Class Var';
      OnStack := 'On Stack';
    
      ShowMessage(F1);
      ShowMessage(F2);
    end;
    
    end.
    
    0 讨论(0)
提交回复
热议问题