Cannot set breakpoint at Exit statement in Delphi?

Viewed 281

I try to set the breakpoint at an Exit statement, like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
begin
  I := 5;

  if I = 5 then
    Exit;

end;

But when the program starts, the breakpoint will become a red cross which indicates it is not available. Why? I am using the "Debug" version of the project and using "Run" button to debug the project.

2 Answers

Alternatively to using a conditional breakpoint as suggested by HeartWare (the performance hit of that can be significant, depending on how often the code is being executed) you could change your code like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
begin
  I := 5;
  if I = 5 then begin
    asm nop end; // <== put breakpoint here
    Exit;
  end;
end;

This provides you with a convenient place to put the breakpoint.

You can't (reliably) set breakpoints on EXIT (or CONTINUE or BREAK) as these are not statements in the normal sense.

You can, however, set a breakpoint on the IF line and then right-click on the red dot for that breakpoint, select Breakpoint Properties and enter the same condition in the condition field as your IF statement:

Conditional Breakpoint

This will slow down the execution a little bit (more if that conditional breakpoint is within a loop), but will only activate the breakpoint if the condition is met (the breakpoint is actually halting the program every time it is hit, but if it has a condition set, the debugger will evaluate that expression and continue the execution without notifying you unless the condition is met).

Of course, this is a simplified example, but you can usually use conditional breakpoints in these cases to overcome the limitation of not being able to set the breakpoint on the exact EXIT statement.

Related