如何定义一个断点,每当对象字段值发生更改时触发?

16

举个例子,给定以下代码片段,我想定义一个断点,当对象字段值发生更改并可选地在条件(在此情况下为 FalseTrue)上中断时触发。

type
  TForm1 = class(TForm)
    EnableButton: TButton;
    DisableButton: TButton;
    procedure EnableButtonClick(Sender: TObject);
    procedure DisableButtonClick(Sender: TObject);
  private
    FValue: Boolean; // <== Would like to define a breakpoint here whenever FValue changes.
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DisableButtonClick(Sender: TObject);
begin
  FValue := False;
end;

procedure TForm1.EnableButtonClick(Sender: TObject);
begin
  FValue := True;
end;

PS - 你不能在类(接口)内部放置断点,断点必须在实现中。 - Jerry Dodge
那我一定是没理解,首先我不知道“数据断点”具体是什么,其次你在问题中从未提到过这个术语。你只说了“断点”。 - Jerry Dodge
1
请勿在问题中添加答案。如果您想添加答案,请在答案中添加。 - David Heffernan
在setter/getter方法上设置断点,创建一个名为“FValue”的布尔属性? - Martin James
@Martin,但在我的实际情况中,该字段是一个可公开访问的视觉组件,随处可见。因此需要在值更改时设置断点。 - Jack G.
显示剩余2条评论
2个回答

29

在调试器下运行应用程序,

从IDE菜单中选择“运行”,然后在底部选择“添加断点”,接着选择“数据断点...”。

将'Form1.FValue'输入到'地址:'字段中。您也可以在同一对话框中设置条件。


@Jerry: 除非它不起作用。条件断点需要几个任务切换,需要几毫秒来评估,并且如果你正在测试的内容在一个紧密循环中,那么时间可能会很快加起来。当条件断点的开销导致程序执行变慢时,我曾经使用过你的解决方案。(只需记得在完成后将其删除即可!) - Mason Wheeler
@Mason - 根据我的经验,64位调试器(XE2)尤其如此,我尽量避免在64位上进行条件断点。我感觉64位调试器有问题,不仅仅是一些任务切换。 - Sertac Akyuz

3

感谢Sertac的回答和David的评论,以下是一些额外信息。

可以根据数组项的变化情况以及条件来定义断点。

在本例中,数据断点定义如下:

Form1.FBooleans[0] = True

代码片段:

type
  TBooleanArray = array of Boolean;

  TForm1 = class(TForm)
    EnableButton: TButton;
    DisableButton: TButton;
    procedure EnableButtonClick(Sender: TObject);
    procedure DisableButtonClick(Sender: TObject);
  private
    FBooleans: TBooleanArray; // Breakpoint defined here with the condition
  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

constructor TForm1.Create(AOwner: TComponent);
var
  AIndex: Integer;
begin
  inherited;
  SetLength(FBooleans, 3);
  for AIndex := 0 to Length(FBooleans) - 1 do
  begin
    FBooleans[AIndex] := (AIndex mod 2) = 1;
  end;
end;

procedure TForm1.DisableButtonClick(Sender: TObject);
begin
  FBooleans[0] := False;
end;

procedure TForm1.EnableButtonClick(Sender: TObject);
begin
  FBooleans[0] := True; // Beakpoint stops here on condition.
end;

那么你只是发布了由 Sertac Akyuz 的答案所启发的想法,对吗? - Jerry Dodge
@Jerry。根据这条评论:“请不要在您的问题中添加答案。如果您想添加答案,请在回答中添加。”那么我应该把这个信息放在哪里? - Jack G.
@Jerry,有什么问题吗? - David Heffernan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接