Thread: Creating new variables causes drawing to completely stop working

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    Creating new variables causes drawing to completely stop working

    This very weird problem is driving me nuts. If I create new variables, all drawing stops working even though I don't make any changes to this part of my code. If I create still more, drawing works again as if nothing happened. Can anyone explain this? This only happens when variables are initialized upon declaration and, stranger yet, only in a certain area. The variables are not even used or referenced, just created. This problem is very weird and seems to point to a bug in VC++ 2008 Express. Here are plenty of examples of what I'm witnessing. For starters, this bit of code has drawing working fine:

    Code:
    ... includes, defines, etc. ...
    ... other variables ...
    int GoodZone = 4;
    int StillGoodZone = 8;
    ... more variables ...
    If I add more variables, not bothering to reference them or anything, just initialize, all of a sudden, drawing stops working.

    Code:
    ... includes, defines, etc. ...
    ... other variables ...
    int GoodZone = 4;
    int StillGoodZone = 8;
    int BadZone = 12;
    int StillBadZone = 16;
    ... more variables ...
    Weirder yet, if I add even more variables, things work fine again:

    Code:
    ... includes, defines, etc. ...
    ... other variables ...
    int GoodZone = 4;
    int StillGoodZone = 8;
    int BadZone = 12;
    int StillBadZone = 16;
    int BackToGoodZone = 20;
    ... more variables ...
    Stranger yet that, if I move these variables to another section of the code, everything still works without problems:

    Code:
    ... includes, defines, etc. ...
    int GoodZone = 4;
    int StillGoodZone = 8;
    int BadZone = 12;
    int StillBadZone = 16;
    ... other variables ...
    ... more variables ...
    Getting even weirder, if I use an array instead and initialize it, everything still works:

    Code:
    ... includes, defines, etc. ...
    ... other variables ...
    int TestArray[4] = {4, 8, 12, 16};
    ... more variables ...
    Shrink the array to size 3 and things stop working. Shrink the array to size 1 and things work again. Increase the array size to 10 and things stop working again. Increase it to 12 and thing work again. Even as far out to 25, things still work. If I don't initialize the array, it always works regardless of the size.

    Even weirder yet is that, if I declare the variables, don't initialize them upon declaration, and set them in a function, I have no problems.

    Code:
    ... includes, defines, etc. ...
    ... other variables ...
    int GoodZone;
    int StillGoodZone;
    int BadZone;
    int StillBadZone;
    ... more variables ...
    
    void InitializeVariables()
    {
       GoodZone = 4;
       StillGoodZone = 8;
       BadZone = 12;
       StillBadZone = 16;
    }
    If I use a different variable type, like the char or __int64, this problem still occurs in the same way, though it takes a different number of variables before this happens, pointing to memory alignment issues.

    Code:
    ... includes, defines, etc. ...
    ... other variables ...
    short GoodZone = 4;
    short StillGoodZone = 8;
    short BadZone = 12;
    short StillBadZone = 16;
    // short BackToGoodZone = 20; // uncomment this to make drawing fail
    ... more variables ...
    Does anyone have any explanations on this? There seems to be an 8-byte zone where things stop working. Could there be a bug with VC++ 2008 Express or a memory leak that I have no idea if it exists or not? Unfortunately, I have no idea how to find memory leaks outside the whole program crashing or looking/behaving weird.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There might be memory leaks in your code, but I doubt if those are the source of this problem. More likely, you are doing things like accessing an array out of bounds or using an invalid pointer that coincidentally points to memory at or near to the memory allocated for these variables in question.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    99.9%, this is a bug in your code.

    The way to find this is to run the code in the debugger, and put a data access breakpoint on one of your bad variables.

    Then when whatever tries to read/write to a location which is supposed to be unused (or you would like to use but it keeps being trashed), you'll have a nice stack trace to the specific instruction performing the access.

    My guess it will be something using a pointer it shouldn't be.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Data access breakpoint? How do I make one of those? Clicking on the line number only makes standard breakpoints that bring out the standard debugger, stopping execution at that point for line-by-line execution. How would I make and use data access breakpoints?
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    An all too easy STFW reveals Breakpoints
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    The option is always grayed out so I can't seem to use the "data breakpoint" option. I forgot to mention that in my previous post. I've done a little test as well that might give some clues, though I don't know if it's a valid test:

    Code:
    // the same stuff as the second above
    ...
    // in some function, the main loop for example (not necessarily the main function)
    if ((BadZone != 12) || (StillBadZone != 16)) // they must be unchanged
    {
       DebugTest = 4; // breakpoint set on this line
    }
    ...
    Both new variables being checked are not triggering the breakpoint. These are newly created variables too so nothing references them except this if statement. I have over 1000 variables in my project so I have no idea how to find out where the memory leak is occurring or even on which variable.

    Oh, and another thing that's interesting is that, if I change both initialized values to 1 instead of 0, drawing works. If one of them is still a 0, then drawing doesn't work.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ulillillia View Post
    I have over 1000 variables in my project
    The first thing I do in every new project is declare 1000 assorted ariables. Then I write as many lines of code as I can before ever attempting to compile. All good programmers do this! It looks like you're on the right track!


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I just add the variables in as I need them. I'm not going to know what the names or sizes are or anything until I need them. Trying to add in the animated clock icon to my game led me to need to create 4 more variables. The first 2 worked fine but the last 2 caused the drawing to stop. I had to add another to get drawing to work again so I can get this element added in and it's now working as I'm expecting. Delete the extra variable and I'm back to the all-black screen.... Put the extra variable in another spot and I still don't get drawing to work as I expect.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps you could use WinDbg instead?
    I've certainly used it in the past to track down data access problems.
    All the really powerful stuff is from the command line (where else).

    > I've done a little test as well that might give some clues, though I don't know if it's a valid test:
    The problems here are
    1. you only get some idea of where the problem is.
    2. every time you move the goal posts to try and get nearer to the problem, the problem itself might move as well.

    Maybe you could do some reading of the help, or searching the web to find out why your GUI debugger doesn't do data breakpoints. Maybe it's because it's the free one - who knows.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Where would I find the info in WinDbg to find out what part of the process is causing data to be written where it's not supposed to be within the program? I'm looking at stuff for memory leaks. I don't know what the process ID is for my program, all I know is its exact file name - "Platform Masters.exe". Since this problem starts immediately upon starting up my program, this means the problem is most likely occurring sometime during the loading phase, done first thing in the program. During this phase, the numerous (a few hundred) images are loaded into memory. A terrain data map is also loaded from an external file into memory. Thus, it makes the most sense that the problem is occurring during startup of the program. I've tried changing the default world that gets loaded, but no matter what I use, it's always the same results. From memorizing my menus (which require drawing in order to see), I attempted to change worlds but nothing happens.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    WinDbg
    http://windbg.info/doc/1-common-cmds.html
    Are you reading anything, or just asking more questions?

    Roughly,
    run the debugger
    load the program - make sure it says "symbols loaded..." for the actual program itself
    set an access breakpoint, say
    ba r 4 myvar
    or
    ba r 4 &myvar
    run, and wait for the breakpoint.

    Feel free to experiment with a 10-line program just to get the hang of it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I was searching the MSDN library. I do see "symbols loaded" for the actual program during the build phase. I still cannot find a way to set any type of breakpoint besides the standard execution breakpoint. Searching MSDN tells me that I should see 4 tabs for the type of breakpoint, but I don't see any tabs. The "data breakpoint" option is grayed out no matter what I do. I'm beginning to wonder if this is unsolvable with the express edition....
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ulillillia View Post
    I have over 1000 variables in my project so I have no idea how to find out where the memory leak is occurring or even on which variable.
    If you are declaring over 1000 variables like this...
    Code:
    ... includes, defines, etc. ...
    ... other variables ...
    int GoodZone;
    int StillGoodZone;
    int BadZone;
    int StillBadZone;
    ... more variables ...
    
    void InitializeVariables()
    {
       GoodZone = 4;
       StillGoodZone = 8;
       BadZone = 12;
       StillBadZone = 16;
    }
    at global scope... trust me the problem isn't the compiler... it's your coding technique.

    Global variables are some serious bad juju and should be avoided whenever possible...
    Why? Because you start getting silly stuff just like you're getting.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    @ulillillia
    As far as I know, windbg is fully featured and uncrippled by marketroids "express" and "professional" market segmentation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Why are global variables bad? I'm using as few as I can. The bulk of the global variables is image data. These are in the form of arrays. About 100 to 150 of the others are actual variables, nearly all of which initialized. I use globals when multiple functions need to reference them. For example, the time limit in my game. The function for drawing the animated clock icon requires that I know how much time is remaining against how much time was permitted. Another function will be referencing it so that a death will occur when time runs out (and back to the start platform you go). I sometimes use it for debugging, for conditional breakpoints. There are other such cases as well. I've got about 3 times more local variables than I do globals.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble sort isnt completely working
    By jamort in forum C++ Programming
    Replies: 6
    Last Post: 08-09-2010, 03:05 AM
  2. Creating a window causes a trackbar to stop working?
    By kidburla in forum Windows Programming
    Replies: 2
    Last Post: 09-20-2007, 05:44 AM
  3. gameports stop working
    By tyler4588 in forum Tech Board
    Replies: 3
    Last Post: 05-26-2005, 10:38 AM
  4. keyboard stop working at Dos
    By arian in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2004, 02:32 PM
  5. Pause in OpenGL, but don't stop drawing... ?
    By Arker in forum Game Programming
    Replies: 4
    Last Post: 10-16-2003, 10:34 PM

Tags for this Thread