Did you know… You can set conditional breakpoints? – #239

This was tip #16 for my TechEd VS IDE Tips and Tricks talk.

I believe this has been around since 2005, but maybe the white ‘+’ is new for 2008?

When you only want to break under certain conditions, you can right-click on a breakpoint red circle (or go to the Breakpoints Window and bring up the context menu on a given breakpoint) and select Condition to bring up the dialog box for conditional breakpoints. 

Conditional Breakpoints on Context Menu

You’re given two options: break only when the specified expression is true or break only when the specified value has changed.  For this example, since I’m in a for loop, i’ll break when the value of i > 5.

breakpoint condition message box

You’ll notice that the breakpoint circle now has a red plus on it to indicate it is conditional.

conditional breakpoint icon

Technorati Tags: VS2005Tip,VS2008Tip

Comments (13)

  1. Csabi says:

    How about strings? How can I set a condition on a string variable, e.g. I want to stop when strTmp == "User3"?

  2. Kelvin says:

    Great post Sara. The white "+" is also in VS2005, but it only shows up when using the "Is True" option.

  3. around since 2005? Mind you, I think Turbo C++ 2.0 already had conditional breakpoints circa 1988. And Visual C++ 4.1 (The first version I used) for sure had them.

    The + is indeed a nice cosmetic… plus.

  4. John says:

    Thanks!  I wish I would’ve known about this one a long time ago!  FWIW, the white ‘+’ is in 2005.

  5. Chris adf471587879rzq says:

    FYI, the white "+" is present in VS 2005 as well.  Cheers!

  6. Kirk says:

    Hi everyone,

    Remember to use == for a comparison, not just a single =. I spent ages trying to figure out how one of my variables was being updated! 🙂


  7. Graham says:

    I have VS2008Express… i only get two options when i right click a breakpoint – disable and remove.

    Is this not in express?  or only in express SP1?

  8. Anastasio says:

    Shouldn’t it really be "You can make breakpoints conditional?".

    A "conditional breakpoint" would be more like the ability to get the program to break when a condition is met at whatever line it happens on. Unfortunately, breakpoints must be set on a particular line, not a condition.

    For example, I would like to set a breakpoint when a button on my form changed it’s .enabled property to False, but I don’t know when that happens (have many statements that can change it).

    Maybe the next VS version?

    Thanks for the great tips though!!

  9. GregM says:

    Anastasio, you can set data breakpoints in VC++ (break when this variable changes, break when this address changes), not sure about about form control properties.

  10. My latest in a series of the weekly, or more often, summary of interesting links I come across related to Visual Studio. I’ve been on vacation in Alaska for 10 days so this is an extra long list of links. Sara Ford: Tip of the Day #236 covers the file

  11. Greg Young says:

    This is quite useful but be wary that the way this works is you still actually hit a breakpoint long enough for the expression to be evaluated by the debugger. This is important when you deal with code that is in a tight loop as it will be incredibly slow …

    To work around this you will tend to have to write your conditional right into your code.


  12. Alvaro says:

    This is useful, but unfortunately it’s not a complete solution.  What’s missing is the ability to break when a certain condition is true on the line that caused it to be true.  Sometimes I spend a long time trying to figure out who changes a static variable.  It would be nice to just say, "whenever this condition is met, stop the program and show me who did it."  

    Visual C++ had this feature ages ago!