Jump to content
Lars Fosdal

Bug: Local variables with duplicate names shows wrong value in debugger

Recommended Posts

https://quality.embarcadero.com/browse/RSP-23056

 

Debugger watch / evaluate shows wrong values for inline declared local variables with duplicate names within same routine.

Put a breakpoint on each of the lines with breakpoint in the comment.
Run
at A, hover over variable s, or evaluate s shows value A
at C, hover over variable s, or evaluate s shows value A - should have been C
The actual value is C - so it is a debugger bug

program ReusedLocalVariables;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  TTest = class
  public
    procedure Check(const aValue: String);
    procedure Test;
  end;

procedure TTest.Check(const aValue: String);
begin
  Writeln(aValue);
end;

procedure TTest.Test;
begin
  begin
    var s := 'A';
    Check(s);   // Breakpoint: S shows 'A' - Output in check = 'A'
  end;
  begin
    var s := 'C';
    Check(s);   // Breakpoint: S shows 'A' - should have been 'C' - Output in check = 'C'
  end;
end;

begin
  try
    var Test := TTest.Create;
    try
      Test.Test;
      Test.Free;
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  finally
    Write('Press Enter: ');
    Readln;
  end;
end.

 

  • Like 1
  • Sad 1

Share this post


Link to post

This really should be fixed! Debugging this code structure will be more common as soon as 10.4 will fix inline declaration or when LSP will be released.
 

procedure TTest.Test;
begin
  if SomeCondition then
  begin
    var Qry := 'select * from sometable';
    Check(Qry);   // Breakpoint: S shows 'A' - Output in check = 'A'
  end;
  {..}
  if SomeOtherCondition then
  begin
    var Qry := 'select * from sometable where condition';
    Check(Qry);   // Breakpoint: S shows 'A' - should have been 'C' - Output in check = 'C'
  end;
  {..}
  while SomeCondition do begin
     var Qry := SomeFunction( SomeParameter );
     Check(Qry);
  end;
  
end;

I'm not saying this construction is good or not, but if the new feature allows it, should work as advertised!

 

Don't have much hair left to pull out.

 

  • Like 4

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×