[RESOLVED] conditional breakpoint with string does not work in visual studio 2010 as I expect
First, I read the following blog post and my expectation is that I can now do some conditional breakpoints with strings provided that I use the list of allowable functions. I could not get it to work at all when using CString objects because the debugger would complain about invalid expression. However, I was able to set a conditional breakpoint with a std::string within the below example. The debugger started without complaining, but the breakpoint never worked. If anyone has done this before, then please explain to me what I am doing wrong with the conditional expression. I've also attached a .jpg with a screen shot where you can see that the breakpoints are set, but the conditional one never worked.
Here is the example code.
Code:
#include "stdafx.h"
#include <string>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::string str;
while(std::getline(std::cin, str))
{
std::cout << "you wrote - " << str << std::endl;
if(str == "stop") // here I create conditional breakpoint strlen(str.c_str()) != 0 EDIT added the .c_str() but still didn't work
break;
}
return 0;
}
EDIT: Here is the website. I forgot to actually paste the link in the original! It is odd that when I search for this information that only a blog post shows up along with a bunch of threads from other websites. I couldn't find anything useful within an official microsoft website or MSDN docs. http://blogs.msdn.com/b/habibh/archi...eakpoints.aspx
Last edited by kempofighter; October 30th, 2013 at 12:52 PM.
Re: conditional breakpoint with string does not work in visual studio 2010 as I expec
In hindsight, I realized that the condition above isn't quite right so I tried this one.
Code:
strcmp(str.c_str(), "stop") == 0
I'm surprised that the previous condition that I tried didn't cause an error because when I tried using the _tcscmp with CString in a different application, visual studio did recognize and complain when I started debugging with in invalid conditional breakpoint. In any event, that didn't work either. The breakpoint still doesn't work even when str is in fact equal to "stop".
Re: conditional breakpoint with string does not work in visual studio 2010 as I expec
See my subsequent reply. It still doesn't work with .c_str() added. Additionally, visual studio didn't complain about whether the expression was valid or not. Also when I tried doing this with CString objects visual studio does provide an error feedback.
Code:
_tcscmp(cstringStr, "anything") == 0 // tried doing that with an MFC CString in a variety of ways and it always resulted in an error from visual studio.
Re: conditional breakpoint with string does not work in visual studio 2010 as I expec
Originally Posted by kempofighter
See my subsequent reply. It still doesn't work with .c_str() added. Additionally, visual studio didn't complain about whether the expression was valid or not. Also when I tried doing this with CString objects visual studio does provide an error feedback.
[CODE]_tcscmp(cstringStr, "anything") == 0 // tried doing that with an MFC CString in a variety of ways and it always resulted in an error from visual studio.
I have never used conditional breakpoints for strings, but here is what I see from all of this:
A CString's type depends on the build, whether it is MBCS or Unicode. So if the build is Unicode, comparing a CString like this:
Code:
_tscmp(cstringStr, "anything")
will never be 0, since "anything" is an ANSI string, not a Unicode string. The _T or TEXT() macro is used to "lengthen" the literal into a Unicode string (if the build is Unicode).
Re: conditional breakpoint with string does not work in visual studio 2010 as I expec
John, that suggestion worked for me. Thanks. That at least proves that it is possible to use some of those c-run time functions if we can figure out what the debugger can handle. I wish I could find some more detailed documentation and examples of what is and isn't possible because the debugger doesn't always tell you when the expression won't work. It just silently fails.
Re: conditional breakpoint with string does not work in visual studio 2010 as I expec
That is a good point which I had forgotten about. Sometimes I see code like this.
Code:
CString csValue(_T("TEST")); // is it necessary to wrap the ansi string?
If I had just written this,
Code:
CString csValue ("TEST"); // then would the _tcscmp(csValue, "test") make sense
I tried this example using CString objects and couldn't get the breakpoint to work. In this case I am not using TCHAR at all so strcmp should work shouldn't it? By the way, _tcscmp does not work and results in a pop-up error that indicates so. Therefore the list of functions indicated within that blog must have shrunk since the beta version. I'm not sure why they would do that considering that so many people use the TCHAR and _T wrapper in their programs, but I digress.
I think that my example does what the blog example does so I don't get it. I cannot find any other documentation within microsoft websites which is strange considering that this seems like it would be a useful new feature. The only thing left for me to ask is whether anyone else can find some detailed debugger documentation or write in some examples in a reply. Perhaps there are some other readers out there that found this feature useful and figured out the specifics of how to use it.
Re: conditional breakpoint with string does not work in visual studio 2010 as I expec
Unless you are intending to build your program both for Unicode and for single/multi-byte, the use of the various _T macros and _t functions just complicates the program. I don't use them with my programs as I don't need to support both in the same source compiled 2 different ways.
Code:
CString csValue("TEST");
This will only work with non-Unicode builds. If you aren't building for Unicode, then this is fine and you then can just use strcmp.
When you pass csValue to a _tcscmp (or strcmp), you are making an inline function call to an operator that converts the CString to the string type that matches the Character Set in the project settings.
This won't work in the debugger conditional break point.
You can do the same thing with a CString that you did with the std::string - assign the value to a local pointer.
After assigning debugStr, you can use the conditional breakpoint as before.
The issue is that you can't call inline functions from the conditional breakpoint. The conversion operator used by CString to allow passing a const char * is an inline function.
There are a number of rules on what is allowed in a conditional breakpoint. These are similar to the rules for native C++ expressions that are documented on MSDN: http://msdn.microsoft.com/en-us/libr...___expressions
The rules vary depending on the version of the studio - there is a dropdown to select the version on this page.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.