Known bug?

I build a GUI and the thing was working perfectly fine. I mean everything was working perfect.

On a given moment I changed something and one variable suddenly changed of value. Which was physically not possible.

I was programming OO style and I triple checked everything. But there was not much to check because I was only working in just one class. One of the things I made was a keyboard on which I can click with my mouse.

I made every button object in a for loop using arrays, I had a an Xpos, Ypos, size, String and char array. It worked flawlessly and I have never touched these arrays again.

Even though these arrays were unchanged all the time, pressing a button would result in sending the wrong character after I made some insignificant change. Using the print() function all over my program I could monitor the value of the character. The character was only mentioned 4 or 5 times and there was no possible way it could receuve a new value.. atleast not by my code.

I tripple checked the entire program for the use of this character, I checked if I did not declared it twice in different classes, I checked if all the {} were correct but nothing, I am and an experienced programmer and I am not mad, but there was something happening which could not be.

I make those button objects one time where the character receive their value. But yet print(c); would return me a different value in one part my program. Even though there was no possible code which could affects it's value, it did chang.

I do not know how, but somehow the function 'mousePressed' was involved, I am positive. That function was already behaving odd with my if-else constructions and I cannot ever get it to work with a while statement.

Unfortunately out of desperation I changed my code so that it actually works and I no longer have the 'proof' to back my sayings up. It took me half an hour before I realized I should have safed it :(

Does anybody also have such issues where 'mousePressed' magically corrupted your program?

Answers

  • Even experienced programmers make some silly mistakes....

    Why not try recreating the problem again and post the code?

  • And what do you mean by

    That function was already behaving odd with my if-else constructions and I cannot ever get it to work with a while statement.

    Have you any code that shows this problem?

  • Re: bask185 --

    which was physically not possible

    magically corrupted your program

    after I made some insignificant change

    This is very, very unlikely. Please provide the source code for feedback.

    Sometimes we think we didn't change something -- but we really did. We think we made an insignificant change ... but it was significant. We may be sure we know what a change does -- but when we run it, we are wrong, and we are surprised.

    Save versions of your work as you go so that you can compare old and new versions. Share your source code if you need debugging feedback.

  • edited February 2017

    Been there a million times and it was always the same ... I made some simple logic error. (A logic error is one that allows the code to compile, allows the compiled code to run but produces unexpected results)

    The problem with this type of error is that it is very hard to find the cause, you stare at the code so much that you get 'tunnel vision', you read the code you expected to see rather than what is there.

    It is so easy to think it must be caused by some underlying bug in the libraries being used (e.g. Processing, Java) but the fact that you had it working perfectly and now it doesn't, suggests strongly that your insignificant change was significant after all.

  • Or maybe you just deleted some important character somewhere when making changes (it's happened many times to me)... We absolutely need to see some sort of code to help you in any way.

  • I use processing to make an application for an HMI on work so I neither have time nor motivation to recreate the bug. Besides the application is finished and working now.

    But to get something productive I do have one question, how is it that while(mousePressed == true) // or false does not do what I want it to do? I suppose it has something to do with the difference between the loop speed and the keyboard refresh speed. Everytime I try to use a while loop with the mouse/keypress function --> not working as intended

  • And that's really the mistake. Where are you trying to put that conditional? In draw? You'll fail. Draw itself is an infinite (with break conditions, etc) loop, so mousePressed variable only updates once before draw.

  • I wanted to post my program but it is 4531 characters too long -.-" too post it here.

    Anyways I was not using while(mousepressed) because I know that never works.

    At the moment of the bug the variable C was used 4 times in the program. The declaration, the initialisation in the constructor, and one time when it was to be printed over the serial port. And the 4th use was print(c); which I would transport all over the program to track it's value. There was a 5th value in which the variable could be changed, that was for using upper or lower cases, but during the bug I outcommented that function and it had no effect.

    1

    int c;

    2

    Button(int Xpos, int Ypos, int Xsize, int Ysize, String text, int arrow, int c) { // regular buttons which directly sends bytes to the machine
        this.Xsize = Xsize;
        this.Ysize = Ysize;
        this.arrow = arrow;
        this.text = text;
        this.c = c;
    
        Xmin = Xpos; 
        Xmax = Xpos + Xsize;
    
        Ymin = Ypos; 
        Ymax = Ypos + Ysize;
      }
    

    3 and 4

     if(mousePressed == true && mouseButton == LEFT && mouseX > Xmin && mouseX < Xmax && mouseY > Ymin && mouseY < Ymax) {    
          if(PIGS_CAN_FLY == true) {    // sends a character, if statements make sure that one char is send per button press
            if( c != 0){ // in new function c gets used slightly more
              serial.write(c); 
              println("  " + hex(c));
              PIGS_CAN_FLY = false;
            }
          }
    

    now it works fine, because I drastically changed the function in which it gets written over the serial port. Now I send a byte when pressing a button rather than releasing it. But this is how it ~looked like. and println(" " + hex(c)); would only give an odd value in the function where c was to be send. I could copy paste the print all over the program and it would display the correct value.

    I usually concur with the statement that you always make some mistake, but the variable c was mentioned so few, that that was out of the question, that is why I came here to ask if others have ever encountered such an unexplainable thing.

  • Am I correct in assuming that all the code that you show (1, 2, 3 & 4) are in the Button class and that you have no other variable called c?

  • I'd use the mousePressed function if I were you.

  • I had similar problem.
    Solved by setting only a boolean value in the mousePressed/mouseReleased function and doing the other instructions within draw().

Sign In or Register to comment.