quant quant - 1 month ago 16x
C++ Question

Conditional breakpoint: This expression has side effects and will not be evaluated

I have a non-static const method called

size_t A::m() const
, which I want to use to trigger a breakpoint if it returns a value greater than 1. Here is
class A
and instance

class A
std::vector<double> myvec;
size_t m() const
return myvec.size();
} a;

So I add a breakpoint in Visual Studio 2013 with this condition

a.m() > 1 // a is an instance of class A

However, when I try to compile this I get the following message from the IDE:

The following breakpoint cannot be set:

At myFile.cpp, line xxx, when 'a.m() > 1' is true

This expression has side effects and will not be evaluated.

Note that
does not modify anything, it only calls the
method of a vector and returns that value, so the assertion that the expression has side effects is simply false. In fact, replacing the breakpoint condition with
a.myvec.size() > 1
(ie. the content of the method itself) has the same effect!

Regarding what can be used as a conditional in a breakpoint, Microsoft says that;

The condition can be any valid expression that is recognized by the

So I went and had a look at Expressions in the Debugger, and found this:

One common cause of side effects is evaluating a function call in a
debugger window. Such evaluations are usually noticeable. A more
subtle cause of side effects is the evaluation of properties and other
implicit function calls in managed code.

The debugger cannot tell whether a property evaluation or implicit
function call has side effects. Therefore, by default, the debugger
does not evaluate implicit function calls automatically. Property
evaluation is allowed by default, but can be turned off in the Options
dialog box. When a function call or property has not been evaluated, a
refresh icon appears. You can manually evaluate the expression by
clicking the refresh icon. For details, see How to: Refresh Watch

When evaluation of properties or implicit function calls is turned
off, you can force evaluation by using the ac format modifier (for C#
only). See Format Specifiers in C#.

If someone could translate the above paragraph to English that would be great. Can I put functions in these debugger conditionals or not?


Here's my translation of the help link you provided:

  • Paragraph 1: calling functions probably has side effects. Evaluating properties might have side effects.
  • Paragraph 2: the debugger can't tell if there are side effects, so we're just going to assume: "functions = bad", "properties = good" (i.e. "functions have side effect, properties do not"). Refresh icon information that isn't relevant to the immediate issue.
  • Paragraph 3: want to force the debugger? If you're using C#, put ,ac after your evaluation.

So, what it boils down to is, if you want to call a function in your evaluation and are using C#, put ,ac after it

a->m() > 1,ac

Since you're using C++, I think this boils down to "No functions in your evaluation statements for you!" For the purpose of debugging, you possibly can remove the const from A::m, since the specifier doesn't (shouldn't) have any impact on the logic flow. I'm not even sure if that will work, though.