Strange behaviour in functions

      Strange behaviour in functions

      I have this code:

      Source Code

      1. if (((i < min) || (i > max)) && (!((this.eMin == 0) && (this.eMax == 0))))
      2. {
      3. ErrMsg = VG.memtable_stringhe["L10"].ToString() + "\n" + min.ToString(this.FormatoNumerico) + " - " + max.ToString(this.FormatoNumerico);
      4. ret = CheckResult.INVALID_RANGE;
      5. break;
      6. }
      7. string msgansw = String.Format("i:{0}\r\nmin:{1}\r\nmax:{2}\r\neMin:{3}\r\neMax:{4}\r\nErrMsg:{5}\r\n", i, min, max, eMin, eMax, ErrMsg);
      8. msgansw += String.Format("i < min: {0}\r\n", (i < min));
      9. msgansw += String.Format("i > max: {0}\r\n", (i > max));
      10. msgansw += String.Format("(!((this.eMin == 0) && (this.eMax == 0))): {0}\r\n", (!((this.eMin == 0) && (this.eMax == 0))));
      11. msgansw += String.Format("(((i < min) || (i > max)) && (!((this.eMin == 0) && (this.eMax == 0)))): {0}\r\n", (((i < min) || (i > max)) && (!((this.eMin == 0) && (this.eMax == 0)))));
      12. MessageBox.Show(msgansw);


      As you can see, the function result is true, but for no reason the if condition it's not executed.
      There are no exceptions, and if I execute the same code in debug mode, step by step, it works fine every time, but in runtime it doesn't work.
      I can't understand the reason.
      If I put a Thread.Sleep(100) before the "if" it works well in runtime too, but I can't understand why.
      Images
      • msgbox.png

        3.4 kB, 390×210, viewed 28 times
      I've tried to simplify the if condition this way:

      Source Code

      1. double min = ((double)this.eMin) / mul;
      2. double max = ((double)this.eMax) / mul;
      3. bool outrange = (i < min) || (i > max);
      4. bool excluded = (this.eMin == 0) && (this.eMax == 0);
      5. if (outrange && !excluded)
      6. {
      7. ErrMsg = VG.memtable_stringhe["L10"].ToString() + "\n" + min.ToString(this.FormatoNumerico) + " - " + max.ToString(this.FormatoNumerico);
      8. ret = CheckResult.INVALID_RANGE;
      9. break;
      10. }


      and works fine, without thread sleep an similar.
      It seems that an if condition with many parameters fails the condition.
      Is one of the values set by a different thread? For example in a producer/consumer scenario? Then maybe you have a race condition, i.e. your test is executed earlier than the values are set. By adding the delay or by running the code in debug mode (which is slower) you gain enough time that the value is actually updated by the other thread before the condition is executed. In this case you should add a critical section to avoid access to variables that are not in their final state.

      Do you have a local variable that hides the class member variable of the same name? For example a local variable eMin would hide this.eMin, so that your output would show a different variable and not the one of the condition.

      Another possibility is a compiler error. That the compiler actually generates bad code. Then you should look at the assembler output of this condition. There you might see why it doesn't work with the complex condition.

      Your F&S Support Team
      F&S Elektronik Systeme GmbH
      As this is an international forum, please try to post in English.
      Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

      Post was edited 5 times, last by “fs-support_HK” ().

      Just call

      Source Code

      1. arm-linux-objdump -d <executable>


      Here <executable> is the name of the program you want to disassemble. The result is output on stdout, so you might want to pipe it to "more" or redirect it to a file.

      Instead of the whole program, you can also give a single .o file. This is easier in large programs because you only need to look at the rather small code section of this module and not the whole application.

      Your F&S Support Team
      F&S Elektronik Systeme GmbH
      As this is an international forum, please try to post in English.
      Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.