Most Random Bug

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I have discovered quite a bug in my program. In the program I run some collision checking, output the coordinates, and draw a point where the collision occurred. I have found my collisions to be quite accurate so I decided to stop outputing the coordinates, but when I do that, the collisions stop working. Put the line back in and it works. In the code below, the line with the arrow pointing at it is the line causing it. If the line is in the code, the collisions work perfectly. If I take it out, they stop working right. I don't get it. Has anyone ever had such a weird bug in their programs? How could I fix this?

Code:
void CheckAllCollisions()
{
    for(int i=0; i<myPlayer.GetWeapon(myPlayer.GetCurWeapon()).GetMaxAmmo(); i++)
    {
        if(CheckCollision(myMesh,myPlayer.GetWeapon(myPlayer.GetCurWeapon()).GetBul​let(i),collision[i]))
        {
----------->     cout << "(" << collision[i].GetX() << ", " << collision[i].GetY() << ", " << collision[i].GetZ() << ")" << endl;
            myPlayer.DeactivateBullet(i);
        }
    }
}
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #2
Having the code for collision[i].GetX() & friends could help people reading this Wink
Quote this message in a reply
breilly
Unregistered
 
Post: #3
Agreed. The implementations of GetX, GetY, and GetZ are critical. I suspect that one or all of them has a side effect.

Other things that you can try, if you suspect that it's something quirky about how that specific code is compiling (which I have seen once or twice before) would be to output something that doesn't involve GetX, GetY, or GetZ... also the other way around... assign the results of GetX, GetY, and GetZ (in different combinations if that helps narrow down the problem) into unused variables and don't output them. That should tell you whether it's these methods or the debugging output that is causing the behavior you're seeing.
Quote this message in a reply
Puzzler183
Unregistered
 
Post: #4
And you are keeping the myPlayer.DeactivateBullet(i); even when you remove the cout right? That could be another place with a side effect.

One other thing - have you tried completely rebuilding your project?
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #5
GetX,Y, and Z simply are return statements that return the X,Y, or Z coordinate (as a float value).
Yes I'm keeping the deactivate bullet function when I'm removing the cout.
I haven't tried rebuilding the project yet.
Quote this message in a reply
kberg
Unregistered
 
Post: #6
Exact code can be critical with these problems, I would copy and paste all relevant functions into this forum. Also double check any arrays which may be allocated 'close to' this function. When you have weird errors like this it could be a sign of stack or heap corruption. If you experience intermittent crashes in memory allocation routines like malloc, realloc, or free, that is a strong sign of heap corruption.
Quote this message in a reply
Moderator
Posts: 702
Joined: 2002.04
Post: #7
I'd also suggest a clean compile; although it may seem like "whatever may ail you, clean compile", I've had Xcode throw a fit, compiling code in a comment block before... Huh

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Post Reply