Subtile bug? (Evaluating variables gives unexpected rezult) *Solved*

Started by
2 comments, last by Calin 18 years, 3 months ago
I have a class function that evaluates whether a interface button is being pressed or not. The function seems to work wright with almoust all buttons (and we have 30+ buttons) except a couple of them that won't get pressed. I've plugged in some helper code to figure out what's causing the problems and I got some puzzling rezults. Bellow is the function code and the log output:

HRESULT SubMenuIClass::Update(MouseBuff MsDevice)
{
   if (this->ID == 700)
   {
      Log("!!!!!New update for GShapes submenu \n ");
   }

   if (this->ID == 700)
   {
      if ( ( ( (MsDevice.X > SubmenuIButt[0].LeftEdge) && (MsDevice.Y > SubmenuIButt[0].UpperEdge) ) &&
             ( (MsDevice.X < SubmenuIButt[0].RightEdge) && (MsDevice.Y < SubmenuIButt[0].LowerEdge) ) ) && 
           (MsDevice.LBDown) )
      {
         Log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Menu button should go down now!!!! \n ");
      }
   }
	
//-------------------------	

   for (int ButtonNumber =0; ButtonNumber<NrOfButtons; ButtonNumber++)
   {
      if (!SubmenuIButt[ButtonNumber].NoDown)
      {
          if (this->ID == 700)
             Log("Ready to evaluate Interface button state \n ");

          if ( ( ( (MsDevice.X > SubmenuIButt[ButtonNumber].LeftEdge) && (MsDevice.Y > SubmenuIButt[ButtonNumber].UpperEdge) ) &&
                 ( (MsDevice.X < SubmenuIButt[ButtonNumber].RightEdge) && (MsDevice.Y < SubmenuIButt[ButtonNumber].LowerEdge) ) ) &&
               (MsDevice.LBDown) )
          {   
              if (this->ID == 700)
                 Log("This message never gets printed");

              SubmenuIButt[ButtonNumber].PlaySound();  // Play the buttons sound when the button is being pressed
			
              SubmenuIButt[ButtonNumber].Down = 1;   //  Change the button state to Down;


The SubmenuIButt[x] buttons never get pressed. The output from my log file looks like this:

 !!!!!New update for GShapes submenu 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Menu button should go down now!!!! 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 !!!!!New update for GShapes submenu 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Menu button should go down now!!!! 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 
 !!!!!New update for GShapes submenu 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Menu button should go down now!!!! 
 Ready to evaluate Interface button state 
 Ready to evaluate Interface button state 


Our game has some unsolved memory leaks. Is it posible these memory leaks might be causing the problem? Thanks for any input. Edit by Fruny - code reformated for readability [Edited by - Calin on January 11, 2006 11:26:39 AM]

My project`s facebook page is “DreamLand Page”

Advertisement
Quote:Original post by Calin
Our game has some unsolved memory leaks. Is it posible these memory leaks might be causing the problem?


Unlikely.

If I were you, I would print out the values of the variables involved in the condition.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Seconded. The results you get are perfectly possible, baring any additional constraints in code you haven't posted. Consider the following (code reposted with line numbers):
/* 01 */ HRESULT SubMenuIClass::Update(MouseBuff MsDevice)/* 02 */ {/* 03 */    if (this->ID == 700)/* 04 */    {/* 05 */       Log("!!!!!New update for GShapes submenu \n ");/* 06 */    }/* 07 */ /* 08 */    if (this->ID == 700)/* 09 */    {/* 10 */       if ( ( ( (MsDevice.X > SubmenuIButt[0].LeftEdge) && (MsDevice.Y > SubmenuIButt[0].UpperEdge) ) &&/* 11 */              ( (MsDevice.X < SubmenuIButt[0].RightEdge) && (MsDevice.Y < SubmenuIButt[0].LowerEdge) ) ) && /* 12 */            (MsDevice.LBDown) )/* 13 */       {/* 14 */          Log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Menu button should go down now!!!! \n ");/* 15 */       }/* 16 */    }/* 17 */ 	/* 18 */ //-------------------------	/* 19 */ /* 20 */    for (int ButtonNumber =0; ButtonNumber<NrOfButtons; ButtonNumber++)/* 21 */    {/* 22 */       if (!SubmenuIButt[ButtonNumber].NoDown)/* 23 */       {/* 24 */           if (this->ID == 700)/* 25 */              Log("Ready to evaluate Interface button state \n ");/* 26 */ /* 27 */           if ( ( ( (MsDevice.X > SubmenuIButt[ButtonNumber].LeftEdge) && (MsDevice.Y > SubmenuIButt[ButtonNumber].UpperEdge) ) &&/* 28 */                  ( (MsDevice.X < SubmenuIButt[ButtonNumber].RightEdge) && (MsDevice.Y < SubmenuIButt[ButtonNumber].LowerEdge) ) ) &&/* 29 */                (MsDevice.LBDown) )/* 30 */           {   /* 31 */               if (this->ID == 700)/* 32 */                  Log("This message never gets printed");/* 33 */ /* 34 */               SubmenuIButt[ButtonNumber].PlaySound();  // Play the buttons sound when the button is being pressed/* 35 */ 			/* 36 */               SubmenuIButt[ButtonNumber].Down = 1;   //  Change the button state to Down;

Assume that:
NrOfButtons == 11SubmenuIButt[0].NoDown == trueSubmentIButt[/* 1..10 */] == falseMsDevice.X > SubmenuIButt[0].LeftEdge == trueMsDevice.X < SubmenuIButt[0].RightEdge == trueMsDevice.Y > SubmenuIButt[0].UpperEdge == trueMsDevice.Y < SubmenuIButt[0].LowerEdge == true(MsDevice.X > SubmenuIButt[/* 1..10 */].LeftEdge && MsDevice.X < SubmenuIButt[/* 1..10 */].RightEdge && MsDevice.Y > SubmenuIButt[/* 1..10 */].UpperEdge && MsDevice.Y < SubmenuIButt[/* 1..10 */].LowerEdge) == falseMsDevice.LBDown == truethis->ID == 700

Now we call SubMenuIClass::Update. The conditional on line 3 is true so we print "!!!!!New update for GShapes submenu \n ". The conditionals on line 8 and lines 10-12 are also true, so we print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Menu button should go down now!!!! \n ". Now we start looping for the first iteration ButtonNumber is 0. The conditional on line 22 is false and so we continue to the next iteration of the loop. For all remaining iterations (ButtonNumber = 1..10) the conditionals on lines 22 and 24 are true, so we print "Ready to evaluate Interface button state \n " once on each of the ten iterations. The conditional on lines 27-29 on the otherhand is always false, so we never hit the conditional on line 31 and therefore never print "This message never gets printed".

Enigma
You are both wright. The line 'if (!SubmenuIButt[ButtonNumber].NoDown)' evaluated false for the first 6 buttons. Thanks for helping me out solving this.

My project`s facebook page is “DreamLand Page”

This topic is closed to new replies.

Advertisement