{DCC 警告} W1036 变量“$frame”可能没有初始化?

发布于 2024-12-17 04:32:28 字数 1190 浏览 2 评论 0原文

为什么我在 Delphi XE 中收到此警告的任何想法:

[DCC 警告] Form1.pas(250): W1036 变量 '$frame' 可能尚未初始化

procedure TForm1.Action1Execute(Sender: TObject);
var
  Thread: TThread;
begin
  ...
 Thread := TThread.CreateAnonymousThread(
   procedure{Anonymos}()

     procedure ShowLoading(const Show: Boolean);
     begin  /// <-------------            WARNING IS GIVEN FOR THIS LINE (line number 250)
       Thread.Synchronize(Thread,
          procedure{Anonymous}()
          begin
            ...
            Button1.Enabled := not Show;
            ...
          end
        );
     end;

  var
    i: Integer;
  begin
    ShowLoading(true);
    try
      Thread.Synchronize(Thread,
        procedure{Anonymous}()
        begin
          ... // some UI updates
        end
      Thread.Synchronize(Thread,
        procedure{Anonymous}()
        begin
          ... // some UI updates
        end
      );
    finally
      ShowLoading(false);
    end;
  end
  ).NameThread('Some Thread Name');
  Thread.Start;
end;

我的代码中没有任何地方有变量名称框架或 $frame。我什至不确定带有 $ 符号的 $frame 如何成为有效的标识符。

对我来说,这就像编译器的魔法。

PS:当然,现实生活中的 xosw 除了 Form1、Button1、Action1 之外还有其他名称。

Any ideas why I get this warning in Delphi XE:

[DCC Warning] Form1.pas(250): W1036 Variable '$frame' might not have been initialized

procedure TForm1.Action1Execute(Sender: TObject);
var
  Thread: TThread;
begin
  ...
 Thread := TThread.CreateAnonymousThread(
   procedure{Anonymos}()

     procedure ShowLoading(const Show: Boolean);
     begin  /// <-------------            WARNING IS GIVEN FOR THIS LINE (line number 250)
       Thread.Synchronize(Thread,
          procedure{Anonymous}()
          begin
            ...
            Button1.Enabled := not Show;
            ...
          end
        );
     end;

  var
    i: Integer;
  begin
    ShowLoading(true);
    try
      Thread.Synchronize(Thread,
        procedure{Anonymous}()
        begin
          ... // some UI updates
        end
      Thread.Synchronize(Thread,
        procedure{Anonymous}()
        begin
          ... // some UI updates
        end
      );
    finally
      ShowLoading(false);
    end;
  end
  ).NameThread('Some Thread Name');
  Thread.Start;
end;

I do not have anywhere in my code a variable names frame nor $frame. I am even not sure how $frame with $ sign can be a valid identifier.

Smells like compiler magic to me.

PS: Of course the real life xosw is having other than Form1, Button1, Action1 names.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2024-12-24 04:32:28

在 XE2 中,不存在此编译器警告。
不管怎样,为了让你的方法对编译器来说更清晰一点,试试这个:

procedure TForm1.Action1Execute(Sender : TObject);
var
  Thread: TThread;
begin
  Thread := TThread.CreateAnonymousThread(
    procedure{Anonymos}()


  Type
    TProcRef = reference to procedure (const Show: Boolean);
  var
    i: Integer;
    ShowLoading : TProcRef;
  begin
    ShowLoading:=
      procedure (const Show: Boolean)
      begin
        Thread.Synchronize(Thread,
          procedure{Anonymous}()
          begin
            // Button1.Enabled := not Show;
          end
        );
     end;

     ShowLoading(true);
    ... // and so on.

更新
我刚刚检查了 XE,这确实修复了编译器警告。

我在问题评论中给出的参考解释了警告和框架对象。
所以我猜想 XE 编译器对 ShowLoading 正在创建的框架对象感到困惑。并创建显式引用修复了警告。

更新2

仅展示我的测试供您参考:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Windows,Classes, SysUtils;

procedure Action1Execute;
var
  Thread: TThread;
begin
 Thread := TThread.CreateAnonymousThread(
   procedure{Anonymos}()


  Type
    TProcRef = reference to procedure (const Show: Boolean);
  var
    i: Integer;
    ShowLoading : TProcRef;
  begin
    ShowLoading:=
     procedure (const Show: Boolean)
     begin
       Thread.Synchronize(Thread,
          procedure{Anonymous}()
          begin
            // Button1.Enabled := not Show;
          end
        );
     end;

    ShowLoading(true);
    try
      Thread.Synchronize( Thread,
        procedure{Anonymous}()
        begin
          // some UI updates
        end
      );
      Thread.Synchronize( Thread,
        procedure{Anonymous}()
        begin
          // some UI updates
        end
      );
    finally
      ShowLoading(false);
    end;
  end
  ); //.NameThreadForDebugging('Some Thread Name');

  Thread.Start;
end;

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    Action1Execute;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

In XE2 this compiler warning is not there.
Anyway, to make your method a bit clearer to the compiler, try this :

procedure TForm1.Action1Execute(Sender : TObject);
var
  Thread: TThread;
begin
  Thread := TThread.CreateAnonymousThread(
    procedure{Anonymos}()


  Type
    TProcRef = reference to procedure (const Show: Boolean);
  var
    i: Integer;
    ShowLoading : TProcRef;
  begin
    ShowLoading:=
      procedure (const Show: Boolean)
      begin
        Thread.Synchronize(Thread,
          procedure{Anonymous}()
          begin
            // Button1.Enabled := not Show;
          end
        );
     end;

     ShowLoading(true);
    ... // and so on.

Update :
I just checked in XE, this really fixes the compiler warning.

The reference I gave in the comment to the question explains about the warning and frame objects.
So I guess that the XE compiler is getting confused about the frame object ShowLoading is creating. And creating an explicit reference fixed the warning.

Update 2 :

Just showing my test for your reference :

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Windows,Classes, SysUtils;

procedure Action1Execute;
var
  Thread: TThread;
begin
 Thread := TThread.CreateAnonymousThread(
   procedure{Anonymos}()


  Type
    TProcRef = reference to procedure (const Show: Boolean);
  var
    i: Integer;
    ShowLoading : TProcRef;
  begin
    ShowLoading:=
     procedure (const Show: Boolean)
     begin
       Thread.Synchronize(Thread,
          procedure{Anonymous}()
          begin
            // Button1.Enabled := not Show;
          end
        );
     end;

    ShowLoading(true);
    try
      Thread.Synchronize( Thread,
        procedure{Anonymous}()
        begin
          // some UI updates
        end
      );
      Thread.Synchronize( Thread,
        procedure{Anonymous}()
        begin
          // some UI updates
        end
      );
    finally
      ShowLoading(false);
    end;
  end
  ); //.NameThreadForDebugging('Some Thread Name');

  Thread.Start;
end;

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    Action1Execute;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文