Why do some properties go out of scope in the watch list while others are not?

First of all, sorry for the long code example, but I believe this is necessary to illustrate my problem.

As a help for debugging, I often present the "DebugString" method for my objects, which returns a brief summary of the object. But sometimes my objects are too complex to be optimally displayed on a single line, so I use string lists. Now I would like to use the excellent debug visualizers in Delphi to observe my object. The way I do this is to introduce a property with getter that rebuilds the string list.

This view works, but for every line that I trace, the property goes out of scope, so I have to click the magnifying glass again in the clock window to see the value. Why is this?

To play, create a new console application:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes;

type
  TMyClass = class
  private
    FInternalData : array[0..4] of integer;
    FDebugStringList : TStringList;
    procedure RebuildDebugStringlist;
    function GetDebugStringList: TStringList;
    function GetDebugString : string;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Scramble;
    property DebugStringList : TStringList read GetDebugStringList;
    property DebugString : string read GetDebugString;
  end;

constructor TMyClass.Create;
begin
  FDebugStringList := TStringList.Create;
end;

destructor TMyClass.Destroy;
begin
  FDebugStringList.Free;
  inherited;
end;

function TMyClass.GetDebugString: string;
var
  I : integer;
begin
  Result := 'Object state: ';
  for I := 0 to 3 do
    Result := Result + inttostr(FInternalData[I])+' ';
end;

function TMyClass.GetDebugStringList: TStringList;
begin
  RebuildDebugStringlist;
  Result := FDebugStringlist;
end;

procedure TMyClass.RebuildDebugStringlist;
var
  I : integer;
begin
  FDebugStringList.Clear;

  FDebugStringList.Add('Object state:');
  for I := 0 to 4 do
    FDebugStringList.Add(inttostr(FInternalData[I]));
end;

procedure TMyClass.Scramble;
var
  I : integer;
begin
  for I := 0 to 4 do
    FInternalData[I] := Random(100);
end;

var
  vMyObj : TMyClass;

begin
  vMyObj := TMyClass.Create;
  try
    vMyObj.Scramble;
    vMyObj.Scramble;
    vMyObj.Scramble;
  finally
    vMyObj.Free;
  end;

  readln;
end.
  • Add hours for "vMyObj.DebugStringList" and "vMyObj.DebugString"
  • Place a breakpoint on line 77 (second "vMyObj.Scramble") and run.
  • Click the magnifying glass next to the DebugStringList to get a visualizer.
  • Please note that the visualizer works great :)
  • Go to the next line. Now the visualizer shows that the clock is out of scope.
  • Press the magnifying glass again to see the new state of the object.

Why does the visualizer say that the watch goes beyond? How can i fix this?

PS: , , "DebugString" "DebugStringList" , .

: Delphi XE

2: , . Embarcadero (QC number 98062, , :-)). , , , Embarcadero , , , , . , , : -)

+5
3

, , Scramble. , , . TStrings, - FDebugStringList TStringList .

var
  vMyObj : TMyClass;
  vSL: Pointer;

{$OPTIMIZATION OFF}
begin
  vMyObj := TMyClass.Create;
  try
    vSL := @(vMyObj.FDebugStringList);

:

TStringList(vSL^)

, vSL, FDebugStringList. , , ", , , ".

Pitfall: , . , .

: , , . . Svein.

: ToolsAPI. StringListVisualizer ToolsAPI , RefreshVisualizer "{, )". , "RefreshVisualizer" ToolsAPI ​​ StringListVisualizer. , RefreshVisualizer, , Scramble, . , . , " ".

+4

, , getter - ?

, , , QC.

+1

Delphi ?, Delphi - , FDebugStringList RebuildDebugStringlist, , , , .

( ..), , ? , , .

+1

All Articles