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 a
:
class A
{
public:
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 A::m()
does not modify anything, it only calls the .size()
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 debugger.
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 Values.
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?
If a source file has changed and the source no longer matches the code you're debugging, the debugger won't set breakpoints in the code by default. Normally, this problem happens when a source file is changed, but the source code wasn't rebuilt. To fix this issue, rebuild the project.
Conditional breakpoints allow you to break inside a code block when a defined expression evaluates to true. Conditional breakpoints highlight as orange instead of blue. Add a conditional breakpoint by right clicking a line number, selecting Add Conditional Breakpoint , and entering an expression.
Right-click the breakpoint symbol and select Conditions (or press Alt + F9, C). Or hover over the breakpoint symbol, select the Settings icon, and then select Conditions in the Breakpoint Settings window.
Here's my translation of the help link you provided:
,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.
Evaluating any function call has side effects, because it induces a change of state, either in registers or the stack, or both. This is exactly what the last paragraph you cited is saying.
In your case, both the registers and stack are changed, assuming standard project settings. A call
instruction is used to call m()
, which pushes the instruction pointer to the stack, and the return value is stored in eax
. On top of these two obvious side effects, the stack and registers are also modified by the prologue/epilogue generated for the calling convention of m
.
This can be verified by generating a naked function and using inline assembler, which only modifies registers:
int __declspec(naked) foo()
{
__asm
{
mov eax, 10
jmp ebx
}
}
int main()
{
__asm
{
mov ebx, cnt
jmp foo
}
cnt:
return 0;
}
If you break into the debugger at any point in this application with foo()
in your watch list, it will display 'This expression has side effects and will not be evaluated.'
There are good answers translating the statement but here is a nice hack someone could use.
In your code where you want to add break point, add a line such as
auto debug = a.m()
Set a breakpoint there, and you can add this in condition
debug > 1
Or you could do something like this
if (a.m() > 1)
bool debug = 1;
and can set a breakpoint in debug line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With