How to create a conditional breakpoint using std :: string

Suppose I have this function:

std::string Func1(std::string myString) { //do some string processing std::string newString = Func2(myString) return newString; } 

How to set conditional break when newString has a specific value? (without changing the source)

setting conditions newString == "my value"

does not work, breakpoints disconnected with error "overloaded statement not found"

+67
debugging visual-studio conditional-breakpoint
Nov 16 '09 at 8:38
source share
10 answers

Some searches have not been able to do this in any way. Suggested alternatives are to put the test in your code and add a standard breakpoint:

 if (myStr == "xyz") { // Set breakpoint here } 

Or create your own test from individual character comparisons. Even looking at individual characters in a string is a little risky; in Visual Studio 2005 I had to dig out member variables like

 myStr._Bx._Buf[0] == 'x' && myStr._Bx._Buf[1] == 'y' && myStr._Bx._Buf[2] == 'z' 

None of these approaches are very satisfactory. We better have access to the ubiquitous feature of the Standard Library.

+44
Dec 08 '09 at 0:04
source share

Visual Studio 2010/2012 is much simpler.

To accomplish what you are looking for in ANSI, use this:

 strcmp(newString._Bx._Ptr,"my value")==0 

And in unicode (if newString were unicode), use this:

 wcscmp(newString._Bx._Ptr, L"my value")==0 

There are more things you can do than just compare, you can read about it here:

http://blogs.msdn.com/b/habibh/archive/2009/07/07/new-visual-studio-debugger-2010-feature-for-cc-developers-using-string-functions-in-conditional- breakpoints.aspx

+75
Mar 04 '13 at 7:49
source share

In VS2017 you can do

 strcmp(newString._Mypair._Myval2._Bx._Buf,"myvalue")==0 
+11
Jul 31 '17 at 22:05
source share

While I had to work around this using something similar to Brad's answer (plus using DebugBreak () to break straight from the code), sometimes editing / recompiling / re-executing bits of code is either too time-consuming or simply impossible.

Fortunately, this is apparently possible for use in the actual members of the std :: string class. One way is mentioned here - and although it calls VS2010 specifically, you can access individual characters manually in earlier versions. Therefore, if you use 2010, you can simply use the beautiful strcmp() functions, etc. ( more) , but if you, like me, and still have 2008 or earlier versions, you can come up with a frantic, scary, but functional alternative by setting a conditional breakpoint like:

 strVar._Bx._Ptr[0] == 'a' && strVar._Bx._Ptr[1] == 'b' && strVar._Bx._Ptr[2] == 'c' 

if the first three characters in strVar are "abc". Of course, you can continue to use additional characters. Ugly ... but it saved me a little time.

+8
Apr 22 '11 at 16:18
source share

VS2012:

I just used the condition below because newString._Bx._Ptr (as in OBWANDO's answer) refers to illegal memory

 strcmp( newString._Bx._Buf, "my value")==0 

and he worked ...

+8
Nov 13 '13 at 9:12
source share

@OBWANDO (almost) has a solution , but as numerous comments rightly point out, the actual buffer depends on the size of the line; I see 16 to be the threshold. Pre-checking the size in strcmp in the appropriate buffer works.

 newString._Mysize < 16 && strcmp(newString._Bx._Buf, "test value") == 0 

or

 newString._Mysize >= 16 && strcmp(newString._Bx._Ptr, "ultra super long test value") == 0 
+2
Jul 11 '18 at 9:29
source share

In VS2017, I was able to set the condition as:

 strcmp(&newString[0], "my value") == 0 
+2
Jun 05 '19 at 11:16
source share

In VS2015 you can do

 newstring[0]=='x' && newString[1]=='y' && newString[2]=='z' 
+1
May 5 '17 at 11:17
source share

String comparison works better than character comparison

 strcmp(name._Mypair._Myval2._Bx._Buf, "foo")==0 

This works, but is very inconvenient to use and error prone.

 name._Mypair._Myval2._Bx._Buf[0] == 'f' && name._Mypair._Myval2._Bx._Buf[1] == '0' && name._Mypair._Myval2._Bx._Buf[2] == '0' 
+1
Jul 16 '18 at 19:23
source share

Just like @Richard's last answer, the bottom line also worked in VS2015

strcmp (& newString [0], "my value") == 0

+1
Jul 08 '19 at 9:59
source share



All Articles