dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: [RESOLVED] conditional breakpoint with string does not work in visual studio 2010 as I expect

  1. #1
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    [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
    Attached Images Attached Images  
    Last edited by kempofighter; October 30th, 2013 at 12:52 PM.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,543

    Re: conditional breakpoint with string does not work in visual studio 2010 as I expec

    Quote Originally Posted by kempofighter View Post
    Code:
    #include "stdafx.h"
        std::string str;
        ...
            if(str == "stop") // here I create conditional breakpoint strlen(str) != 0 and I also tried 
                break;
    And what do you expect strlen could return for std::string passed in as a parameter?
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    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".

  4. #4
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    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.
    According to the aforementioned blogpost, the above should work in an MFC app.
    http://blogs.msdn.com/b/habibh/archi...eakpoints.aspx

    (By the way, I forgot to post the blog website that I mentioned before).

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: conditional breakpoint with string does not work in visual studio 2010 as I expec

    Quote Originally Posted by kempofighter View Post
    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).

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jan 2001
    Posts
    253

    Re: conditional breakpoint with string does not work in visual studio 2010 as I expec

    The debugger doesn't make it easy to use inline members of classes in conditional expressions.

    However, you can help it by adding extra code.

    For example, you can create a local variable which you assign with the constant pointer returned by .c_str():
    Code:
    #include <iostream>
    #include <string>
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
       std::string str;
       while (std::getline(std::cin, str))
       {
          std::cout << "you wrote - " << str << std::endl;
          const char* debugStr = str.c_str();
          if (str == "stop")
             break;
       }
       return 0;
    }
    Once you do this, you can put a conditional breakpoint on the if statement which uses the debugStr variable
    Code:
    strcmp(debugStr, "TEST") == 0
    This will stop on the TEST string.

    John

  7. #7
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    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.

  8. #8
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    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.

    Code:
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include <atlstr.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        std::string str, stop("stop");
        while(std::getline(std::cin, str))
        {
            std::cout << "you wrote - " << str << std::endl;
            const char* debugStr = str.c_str();
            CString csDebugStr(str.c_str()), csStop("stop");
            if(str == "stop") // tried both strcmp(csDebugStr, "stop") == 0 and strcmp(csDebugStr, csStop) == 0
                break;
        }
    	return 0;
    }
    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.

  9. #9
    Join Date
    Jan 2001
    Posts
    253

    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.
    Code:
    CString csDebugStr = "Some string";
    const char* debugStr = csDebugStr;
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)