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 E2313 Attribute – Known attribute cannot specify properties Reason for the Error & Solution No further information...
Delphi Compiler Error E2379 Virtual methods not allowed in record types Reason for the Error & Solution No further information...
Rodrigo , one of the long time Delphi Developer has been working on one of his personal project “Delphi IDE...