Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a conditional breakpoint in Visual C++

I want to add a breakpoint condition to my code in VC++ Express 2005, so that the breakpoint only triggers if a local variable meets a specified criteria. e.g.

bool my_test(UIDList test_list) {
    foo(test_list);
    bar(test_list); // I have a breakpoint here, but only want it to trigger if test_list.Length() > 0
    print(test_list);
}

Having right-clicked on my breakpoint and selected "Condition..." I have found a dialog that appears to do what I want, however anything I try typing into the text field results in the following error:

Unable to evaluate the breakpoint condition: CX0052: Error: member function not present

I tried the help documentation, but I couldn't find my answer. I'm hoping someone experienced in VC++ might be able to point me in the right direction...

I have previously tried upgrading to a more recent version of VC++ Express, but the project did not import cleanly. Due to the complexity of the project and my current time scales I can't consider upgrading as a solution at this point.

like image 612
seanhodges Avatar asked Mar 18 '09 10:03

seanhodges


People also ask

How do you do a conditional breakpoint?

To set a conditional breakpointOn the Home tab, in the Breakpoints group, choose Set/Clear Condition. In the Debugger Breakpoint Condition window, enter a condition. On the Home tab, in the Breakpoints group, choose List. In the Debugger Breakpoint List window, enter a condition in the Condition column.

What is a conditional breakpoint?

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.


2 Answers

VS does have some micro-evaluation engines- in variable watch windows, immediate window, break point conditions etc. I could never find decent documentation on them. As far as i can tell they are picky about methods they're willing to call, but they're also insensitive to access limitations.
So, you can probably rephrase your condition from

test_list.Length() > 0  

to something like

test_list.m_nLength > 0

(or whatever your private length var is).

(EDIT) Just found this msdn page explaining what expressions the debugger can and can't handle. So first, indeed -

'The debugger can access all class members regardless of access control. You can examine any class object member, including base classes and embedded member objects.'

And second, my guess regarding the failure to evaluate 'Length()' - it was probably inlined:

'A debugger expression cannot call an intrinsic or inlined function unless the function appears at least once as a normal function.'

like image 104
Ofek Shilon Avatar answered Oct 14 '22 16:10

Ofek Shilon


use the DebugBreak(); function:

bool my_test(UIDList test_list) {
    foo(test_list);
    if (bar(test_list) /* or whatever check :) */) // I have a breakpoint here, but only want it to trigger if test_list.Length() > 0
        DebugBreak();
    }
    print(test_list);
}

Or you can always use assert(expression)

bool my_test(UIDList test_list) {
    foo(test_list);
    bar(test_list);
    assert(test_list.Length() > 0); // will break here
    print(test_list);
}
like image 32
Stormenet Avatar answered Oct 14 '22 15:10

Stormenet