Thread: No errors when debugging?

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up No errors when debugging?

    I've got a (lengthy) program, and when I run it, some variables get some really bogus values for some reason. When I debug it to find the cause of it, the values are fine, making it difficult to track down the problem.

    So, my question is, what could cause this? Obviously the debugger does something differently. I read somewhere that the debugger gives values to uninitialized variables, though I don't think this is my particular problem.

    So, any ideas?

    Cheers.

    BTW, I'm using MSVS '08.
    Last edited by yaya; 03-06-2010 at 10:01 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I've got a (lengthy) program, and when I run it, some variables get some really bogus values for some reason
    I'd suggest you post your complete code, or at least make a copy of it and strip it down as much as possible, while still reproducing the issue. Also, when you run it how do you know the variables have "bogus" values? Are you using them without initializing them? Either that or some pointer arithmetic is my guess as to where the problem lies.

    Yes, you're correct that debuggers generally do things differently. What it does depends entirely on what compiler/debugger you're using, I imagine your using VC++.

    Again, I'd suggest to post the code (preferably a "small" version of it). Also highlight in the code the "problem" areas, as well as any other details, like input, output, etc. Without seeing any code we can only guess what the problem is (i.e. using without initializing, etc).

  3. #3
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    After looking for a bit, it does seem to be a problem with pointers. To sum up the code a bit, I am accessing a member of a class. When I access it directly, it works fine, but when I use a pointer to it, it doesn't work, even though the pointer itself appears to be legit. Here's a small example:

    Code:
    class FOO
    {
    public:
    	int a;
    };
    
    FOO foo;
    foo.a = 12;
    
    int *bar = &foo.a;
    
    foo.a == 12; // this is true
    *bar == 12; // this is false

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    How do you know what they are evaluated to? Do something like this, for both
    Code:
    if ( foo.a == 12 )
      cout << "true";
    else
      cout << "false";
    If its still acting weird, maybe change
    Code:
    int *bar = &foo.a;
    to
    Code:
    int *bar = &(foo.a);
    but I think it should be OK as is.

  5. #5
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Quote Originally Posted by nadroj View Post
    How do you know what they are evaluated to? Do something like this, for both
    Code:
    if ( foo.a == 12 )
      cout << "true";
    else
      cout << "false";
    Yes, that is sort of how I am doing it, though through a message box. And it is obvious things are going wrong because the program starts doing weird things.

    Quote Originally Posted by nadroj View Post
    If its still acting weird, maybe change
    Code:
    int *bar = &foo.a;
    to
    Code:
    int *bar = &(foo.a);
    but I think it should be OK as is.
    That didn't do it either.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    And it is obvious things are going wrong because the program starts doing weird things.
    Well if that isn't ambiguous....

    I tried the code above, after syntactically fixing it, and adding the print statements I mentioned above and I get the expected output (two "true"s printed). Not sure what else to say. Maybe try creating an entirely new "project" in your IDE, and copy/paste the above small program with basic "cout" print statements and see if that gives expected output.

  7. #7
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Hmm... I guess I'll just keep searching for the problem then. Thanks for your help, though.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Maybe post your exact, complete "small" version that reproduces the problem. The one that "does weird things at startup", etc., and I can try to run it.

  9. #9
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    The problem is, I've got 1000s of lines of code and this particular variable is used a number of times through out it (I doubt I could find them all).

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Working on debug but not release mode suggests that you have some undefined behavior.

    You need to prune your program untill the problem either goes away, and you can attribute it to the last remove; or until you have a minimalistic program that encounters the problem, which you can post here.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    From our discussion above it was my understanding that you wrote a "small" ("small" being relative) program that reproduces the problem. And that you ran that, and still got the "weird behaviour". If you're still running a program with thousands of lines of code, there's, of course, more than a thousand places where the logic error could be.

    Working from your post above (No errors when debugging?) modify that to be an actual program, run it (not debug it!) and tell us the output, i.e. does it print that they are equal or not equal. Of course post all of that complete, small program that you run.

  12. #12
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    No, sorry, that small code a gave you doesn't produce the error (I just tested it), rather, it is sort of what it going on in my actual program. It just seems weird that it is pointing to the same place (I assume), yet the data is different.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I don't think we can do anything if we don't have some code that we are all discussing, which of course reproduces the problem. Maybe turn your compiler warning level up as high/strict as possible and try to fix every single warning.

  14. #14
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Ahh, I figured it out. I changed (or forgot to change) the pointer at one point in the code. But now I'm confused as to why it DID work under debug mode. All well, at least it's working now. Thanks for your help.

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If your code works in debug mode but not in release mode, my first guess would be stack corruption, because in debug mode, VS adds 4 extra bytes of padding before and after your variables; so if you write a few bytes past the end of an array it won`t corrupt other variables the way it does in release mode.
    Check for stack corruption by opening the Memory viewer and step through your program line by line to see if any of the padding characters get overwritten (you should have 0xCDCDCDCD before and after your arrays).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sneaky little linker errors...
    By Tozar in forum C++ Programming
    Replies: 8
    Last Post: 10-25-2006, 05:40 AM
  2. Errors with header files in OpenGL using VisualC++
    By wile_spice in forum Game Programming
    Replies: 3
    Last Post: 06-22-2006, 08:56 AM
  3. Program Fatal Error- Please Explain
    By luckygold6 in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2003, 08:56 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM