can't assign a bool value - causes crash?!?

Started by
9 comments, last by python_regious 18 years, 11 months ago
hey all, alright, the name almost says it all. i've almost finished (ok about 3/4 done) converting all my directdraw functions into direct3d and ive noticed that one tiny area of code now has a problem with this... here's the code ive got it down to:

typedef struct _Corners {
    bool leftup, leftdown, rightup, rightdown;
    void Reset()
     { leftup = false; leftdown = false; rightup = false;
       rightdown = false; }
  }Corners;

//...to the function

Corners incorners;
incorners.Reset();
if ((Images[tile.ImageNum-1].collmaploaded) &&
    (all.collmaploaded && overtile.left > -1) &&
    (overchar.left > -1))
   {
    int colt, rowt, tilex, tiley, charx, chary, x = 0, y = 0;
    POINT charpos;
    charpos.x = 1; charpos.y = 1;
    colt = tile.CellNum; rowt = tile.RowNum;
    charpos = all.GetAnimatePos(all.characters[charnum].movedir);
    charpos.y = 1;
    tilex = 1+(MapData.TileX*colt+colt-1)-MapData.TileX;
    tiley = 1+(MapData.TileY*rowt+rowt-1)-MapData.TileY;
    charx = 1+(all.tilewidth*charpos.x+charpos.x-1)-all.tilewidth;
    chary = 1+(all.tileheight*charpos.y+charpos.y-1)-all.tileheight;
    while ((y+overtile.top < overtile.bottom) && 
           (y+overchar.top < overchar.bottom))
    {
     while ((x+overtile.left < overtile.right) &&
            (x+overchar.left < overchar.right))
     {
      if ((Images[tile.ImageNum-1].CollisionMap
           [x+tilex+overtile.left][y+tiley+overtile.top]) && 
          (all.CollisionMap[x+charx+overchar.left][y+chary+overchar.top]))
      { 
        if (x+tilex+overtile.left < tilex+halfx && 
            y+tiley+overtile.top < tiley+halfy) 
         { incorners.leftup = true; }
        if (x+tilex+overtile.left >= tilex+halfx && 
            y+tiley+overtile.top < tiley+halfy) 
         { incorners.rightup = true; }
        if (x+tilex+overtile.left < tilex+halfx && 
            y+tiley+overtile.top >= tiley+halfy) 
         { incorners.leftdown = true; }
        if (x+tilex+overtile.left >= tilex+halfx && 
            y+tiley+overtile.top >= tiley+halfy) 
         { incorners.rightdown = true; }
      }
      ++x;
     }
     ++y;
     x = 0;
    }
   }
   return incorners;


ok, see how i only try to set the values to TRUE? (ignore the .Reset() call - ive established it doesnt crash it...) if i comment/erase that code (those single lines...), the program works fine. however, when left like it is there, the program crashes the instant any of those bools are assigned a value... i have absolutely no idea why this is happening so if anyone can help at all i'd greatly appreciate it. [edit]Altered brackets for ease of reading[/edit] thanks [Edited by - the_moo on May 16, 2005 8:09:15 AM]
the_moo
Advertisement
Firstly, that code isnt very readable. Means people on here are less likely to try and decypher it.

I would recommend reformatting it and cleaning it up a little. You might realise that you have done something wrong that it. Start by bracketting up the 'if' expressions.

Also i would try a complete clean and rebuild (if you are using Visual Studio).

ace
Using a debugger and stepping through the code works wonders too...
If at first you don't succeed, redefine success.
I really doubt its the assignment thats the problem. Your going out of bounds in memory somewhere, sorry I can't read all of that to track it down, but I bet you thats what it is. Check where you access array indices or dereference pointers and the like.
moe.ron
Your function is really prone to crashes. You should at the very least have an assert before every single array access.

Here is just one example of a situation that can cause crashes through array overflowing:

tile.CellNum == 0
MapData.TileX == 1
overchar.left == 0

Causes an overflow for:

all.CollisionMap[x+charx+overchar.left]



My suggestion: put asserts all around the place and check all the bounds. Once the function is safe from overflows, run through it with a debugger.
thanks for the suggestions everyone, but i assure you that the crash only happens at the lines where i assign true to the bool var.
i can use normal bools, but then i want/need to have that "Corners" struct to return or "build" the right information for the function to give out (like with a referenced parameter) but it won't allow that either - same deal.

i'm really stumped by this problem - i hope someone can shed some more light on it for me...
thanks again
the_moo
This isnt a solution, but you may not assign bool, use unsigned int instead and then see what happens. Are you sure that this is in this function perhaps this is somewhere in other if statement(i.e. if(incorners.leftup) ). Other question is why are you typedef that struct?.
Ok, well then plonk _ASSERTE( _CrtCheckMemory() ); around the code, preferably before and afterwards, just to make sure that your memory isn't screwed before hand, and that the error absolutely is in that code.
If at first you don't succeed, redefine success.
hey again,
python: i tried inserting _ASERTE(...); but it's undefined (looked it up then and found its part of .NET right? - i'm not using the net framework).
in any case take a look at this:
        if (x+tilex+overtile.left < tilex+halfx &&             y+tiley+overtile.top < tiley+halfy)          { //incorners.leftup = true; }         }        if (x+tilex+overtile.left >= tilex+halfx &&             y+tiley+overtile.top < tiley+halfy)          { //incorners.rightup = true; }         }        if (x+tilex+overtile.left < tilex+halfx &&             y+tiley+overtile.top >= tiley+halfy)          { //incorners.leftdown = true; }         }        if (x+tilex+overtile.left >= tilex+halfx &&             y+tiley+overtile.top >= tiley+halfy)          { //incorners.rightdown = true; }         }

ok, so all i've done differently is comment out the bool assignment code yeah?
well, the program runs perfectly when compiled with this minor change. i know there are fancier ways of narrowing down/finding a problem but this worked for me - just trust me k?
i've made two programs to show you the effect of changing just what i showed above:
Download programs
hope that helps you understand the problem so you might have some more suggestions.
thanks again
the_moo
If (you formatted code like this){   I'd be more likely to read it;}


That code obviously isn't complete, but assuming that incorners really is a local variable, try printing out the contents of it just before the if statement. Should be false false false false. I'm betting it's either 'rubbish junk random data' or it crashes on the print statement.

This topic is closed to new replies.

Advertisement