Delphi Error – E2171 Variable ‘%s’ inaccessible here due to optimization

Delphi Compiler Error

E2171 Variable ‘%s’ inaccessible here due to optimization

Reason for the Error & Solution

The evaluator or watch statement is attempting to retrieve the value of <name>, but the compiler was able to determine that the variables actual lifetime ended prior to this inspection point. This error will often occur if the compiler determines a local variable is assigned a value that is not used beyond a specific point in the program’s control flow.

Create a new application.
Place a button on the form.
Double click the button to be taken to the 'click' method.
Add a global variable, 'c', of type Integer to the implementation section.

The click method should read as:

  procedure TForm1.Button1Click(Sender: TObject);
    var a, b : integer;
  begin
    a := 10;
    b := 20;
    c := b;
    a := c;
  end;

Set a breakpoint on the assignment to 'c'.
Compile and run the application.
Press the button.
After the breakpoint is reached, open the evaluator (Run|Evaluate/Watch).
Evaluate 'a'.

The compiler realizes that the first assignment to ‘a’ is dead, since the value is never used. As such, it defers even using ‘a’ until the second assignment occurs – up until the point where ‘c’ is assigned to ‘a’, the variable ‘a’ is considered to be dead and cannot be used by the evaluator.

The only solution is to only attempt to view variables which are known to have live values.

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...