Delphi Error – E2189 Thread local variables cannot be local to a function

Delphi Compiler Error

E2189 Thread local variables cannot be local to a function

Reason for the Error & Solution

Thread local variables must be declared at a global scope.

program Produce;

  procedure NoTLS;
    threadvar
      x : Integer;
  begin
  end;

begin
end.

A thread variable cannot be declared local to a procedure.

program Solve;

  threadvar
    x : Integer;

  procedure YesTLS;
    var
      localX : Integer;
  begin
  end;

begin
end.

There are two simple alternatives for avoiding this error. First, the threadvar section can be moved to a local scope. Secondly, the threadvar in the procedure could be changed into a normal var section. Note that if compiler hints are turned on, a hint about localX being declared but not used will be emitted.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...