Remote debugging with XE2 – display of strings

I’m not entirely sure why you have the {} around the string values, – my hunch is that it is to demonstrate that the values are coming from a remote execution – but I know for a fact that S is being truncated due to optimization…

{$O-} // Disable Optimization
var
  S: AnsiString;
  S2: UnicodeString;
begin
  S := 'Hello';
  S2 := 'Hello2';
  ShowMessage(S2);
end;
{$O+} // Enable Optimization

You’ll now note that the value ‘Hello’ (of variable S) remains intact when debugging. Likewise if you make use of the value assigned to S:

var
  S: AnsiString;
  S2: UnicodeString;
begin
  S := 'Hello';
  S2 := 'Hello2';
  ShowMessage(S + S2);
end;

Delphi’s optimization now identifies that S is being used within its valid scope, and so the value is preserved.

So, what you’re calling a “bug” is in fact a “compiler feature” exactly as Borland/Inprise/Codegear/Embarcadero intended.

Leave a Comment