Recent

Author Topic: Odd behaviour: 0 <> 513 = false !????  (Read 3036 times)

ArminLinder

  • Sr. Member
  • ****
  • Posts: 320
  • Keep it simple.
Odd behaviour: 0 <> 513 = false !????
« on: August 25, 2021, 09:54:54 am »
Hi all,

I have problems with this simple lines of code:

Code: Pascal  [Select][+][-]
  1.     if FConfigurationFileRec.ConfigVersion <> CONFIGVERSION then
  2.       raise exception.create(
  3.         Format('Versionsfehler: die angegebene Konfigurationsdatei hat Version %d.%d, benötigt wird Version %d.%d',
  4.         [Hi(FConfigurationFileRec.ConfigVersion), lo(FConfigurationFileRec.ConfigVersion), hi(CONFIGVERSION),
  5.         lo(CONFIGVERSION)]));

Both FConfigurationFileRec.ConfigVersion and CONFIGVERSION are declared Integer (=LongInt). FConfigurationRec.ConfigVersion contains value zero (0), CONFIGVERSION is $0201 (513), both values checked at runtime using the debugger. Nevertheless the exception never triggers. Stepping with the debugger, the comparison is executed,  but execution continues at the line right after the raise.

I guess, however, that I struggle with a different problem, I think the comparison does actually evaluate to true, but for some reason, because of a problem with the optimization maybe, the raise line gets purged from the code.

Can anyone shed a light on how this code can fail?

Thnx, Armin.
Lazarus 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

Bart

  • Hero Member
  • *****
  • Posts: 5715
    • Bart en Mariska's Webstek
Re: Odd behaviour: 0 <> 513 = false !????
« Reply #1 on: August 25, 2021, 10:08:42 am »
Compile with -O-.
If it runs OK then, your guess (optimization is the problem) becomes a real possibility.

Note: values you see in the debugger anr not always correct.
If you want to know the values @runtime, then write them out to console or a log (before doing the comparison).

Bart

rsz

  • New Member
  • *
  • Posts: 45
Re: Odd behaviour: 0 <> 513 = false !????
« Reply #2 on: August 25, 2021, 10:25:56 am »
Hi,

Could you provide a minimal sample which reproduces the issue?

I guess, however, that I struggle with a different problem, I think the comparison does actually evaluate to true, but for some reason, because of a problem with the optimization maybe, the raise line gets purged from the code.

Can anyone shed a light on how this code can fail?

Bart beat me to it, but did you try building and running without optimizations or try it in debug mode? Does the same thing still happen?
My two initial guesses would be memory corruption or variable shadowing.

Thaddy

  • Hero Member
  • *****
  • Posts: 19139
  • Glad to be alive.
Re: Odd behaviour: 0 <> 513 = false !????
« Reply #3 on: August 25, 2021, 10:48:00 am »
Pascal is case insensitive so things like ConfigVersion and CONFIGVERSION could indeed lead to variable shadowing or scope issues. Try to prepend _CONFIGVERSION or change that const to a different name.
What worries me a bit too is that you use signed integers instead of unsigned, but based on your snippet and the values that is unlikely here. Still, it is a logic error nowadays.
« Last Edit: August 25, 2021, 10:51:31 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

dseligo

  • Hero Member
  • *****
  • Posts: 1683
Re: Odd behaviour: 0 <> 513 = false !????
« Reply #4 on: August 25, 2021, 12:09:43 pm »
Can you try it like this:

Code: Pascal  [Select][+][-]
  1.     ShowMessage(IntToStr(FConfigurationFileRec.ConfigVersion) + LineEnding + IntToStr(CONFIGVERSION));
  2.     if FConfigurationFileRec.ConfigVersion <> CONFIGVERSION then
  3.     begin
  4.       ShowMessage('before raise');
  5.       raise exception.create(
  6.         Format('Versionsfehler: die angegebene Konfigurationsdatei hat Version %d.%d, benötigt wird Version %d.%d',
  7.         [Hi(FConfigurationFileRec.ConfigVersion), lo(FConfigurationFileRec.ConfigVersion), hi(CONFIGVERSION),
  8.         lo(CONFIGVERSION)]));
  9.       ShowMessage('after raise');
  10.     end;
  11.     ShowMessage('after if');

What messages do you see?
« Last Edit: August 25, 2021, 12:12:33 pm by dseligo »

Bart

  • Hero Member
  • *****
  • Posts: 5715
    • Bart en Mariska's Webstek
Re: Odd behaviour: 0 <> 513 = false !????
« Reply #5 on: August 25, 2021, 10:18:03 pm »
Also, you're not in a contruct like this?
Code: Pascal  [Select][+][-]
  1.   with FConfigurationFileRec do
  2.   begin
  3.     ...
  4.     ...
  5.     ...
  6.     if FConfigurationFileRec.ConfigVersion <> CONFIGVERSION then
  7.  
  8.   end;

Bart

jamie

  • Hero Member
  • *****
  • Posts: 7702
Re: Odd behaviour: 0 <> 513 = false !????
« Reply #6 on: August 25, 2021, 11:07:53 pm »
I have seen such behavior when the method that is handling the code gets reentered by some means.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018