Recent

Author Topic: Baffled by an "if" that doesn't work  (Read 4004 times)

Henppy

  • Jr. Member
  • **
  • Posts: 60
Baffled by an "if" that doesn't work
« on: August 30, 2014, 09:22:03 pm »
Ok, so I don't know what's happening here... You know, "the compiler is always right" versus  "I'm right and this thing is doing it wrong!"... This time I swear I'm right. :)

Here's the code, lets see if any of you gentlemen can throw me a bone because I'm lost:

Code: [Select]
procedure TGenericList.CheckIndex(Index: integer);
begin
  if Index >= FItemCount then
      raise EListIndexOutOfBounds.Create('Index ' + IntToStr(Index) + ' outside of bounds [0..' + IntToStr(FItemCount - 1) + ']');
end;

Where FItemCount is:

Code: [Select]
private
     FItemCount: integer;

Please see the attached image, the exception is thrown when Index = 1 and FItemCount = 2.

I don't know how to debug this... so any help is welcome. :D

I'm using 1.2.0 BTW, but as far as I know it's the same FPC compiler version.


Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Baffled by an "if" that doesn't work
« Reply #1 on: August 30, 2014, 09:37:17 pm »
is not possible to reproduce the behavior with the code provided.
but are you sure that FItemCount in the procedure in question is the one with the value 2?
if is the same then

seems like a wrong order on cheking your index
« Last Edit: August 30, 2014, 09:50:07 pm by Never »
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

Henppy

  • Jr. Member
  • **
  • Posts: 60
Re: Baffled by an "if" that doesn't work
« Reply #2 on: August 30, 2014, 09:44:16 pm »
Yep, just quadruple-checked, no other thing called neither FItemCount or Index that could be reached in there.

I can upload the code, problem is that this is happening in the middle of code parsing by SynEdit... So debugging is not as trivial as hitting F9.

Worst thing is that the exception is sort of "swallowed", not thrown to be cached at all,  the program just hangs there.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Baffled by an "if" that doesn't work
« Reply #3 on: August 30, 2014, 09:46:33 pm »
Can you attach the full code for TGenericList?

Henppy

  • Jr. Member
  • **
  • Posts: 60
Re: Baffled by an "if" that doesn't work
« Reply #4 on: August 30, 2014, 09:58:55 pm »
Attached the file in question. Also uploaded the code to: https://dl.dropboxusercontent.com/u/70266302/src.zip

Now, if you're willing to run this thing. First of all thank you, and second, here's what you need to do:

1) The problematic code is in uContainers.pas line #157.
2) Type a letter, debugger will stop but ignore it this time.
3) Press enter so you add a new line after the letter you just added. Debugger stops again and here is where the problem is.

Thanks all.

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Baffled by an "if" that doesn't work
« Reply #5 on: August 30, 2014, 10:01:00 pm »
i will look your code but you might find faster this
[
some where in your code you are doing this

increasing your count var
your cheking the index-->if you are here then you migh have a value 2 in count but is not correct because the item is not added yet
so if you use the count to determine a possition to add your items this will lead to an error

and last you add an item or whatever
]
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

Henppy

  • Jr. Member
  • **
  • Posts: 60
Re: Baffled by an "if" that doesn't work
« Reply #6 on: August 30, 2014, 10:04:06 pm »
I run into this when I was adding a method that would remove items:

Code: [Select]
procedure TGenericList.RemoveFrom(Index: integer);
begin
  CheckIndex(Index);
  FItemCount := Index;
end;

So at this point there's no modification of anything expect the integer FItemCount... that's what I think at least. :P

BTW, the sequence of events would be:

1) The user is adding a line.
2) The SynEdit parser will do its thing, but first, it wants to get rid of any tokens stored on a list. So it calls the RemoveFrom method.
3) RemoveFrom method just decreases the FItemCount field.
« Last Edit: August 30, 2014, 10:06:13 pm by Henppy »

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Baffled by an "if" that doesn't work
« Reply #7 on: August 30, 2014, 10:26:17 pm »
belive it or not your program produces no errors here
 so just close evrerthing reset the debuger or even better reset and reboot and try it
 maybe the debuger is stuck somewhere it happens to me often
the timer is one thing to have your eye on
« Last Edit: August 30, 2014, 10:29:57 pm by Never »
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Baffled by an "if" that doesn't work
« Reply #8 on: August 30, 2014, 10:34:18 pm »
My experience: debugging generics is pretty tricky.

Try to put writeln or MessageDlg right before the condition, to see what really happens there.

Code: [Select]
procedure TGenericList.CheckIndex(Index: integer);
begin
writeln(Index, FItemCount);
MessageDlg(...);
  if Index >= FItemCount then
      raise EListIndexOutOfBounds.Create('Index ' + IntToStr(Index) + ' outside of bounds [0..' + IntToStr(FItemCount - 1) + ']');
end;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Baffled by an "if" that doesn't work
« Reply #9 on: August 30, 2014, 10:48:16 pm »
found another bug
file : uKerboscriptHighlighter
Line:151
Code: [Select]
procedure TKerboscriptHighlighter.SetLine(const NewValue: string; LineNumber: integer);
begin
  inherited;
  CurrentLine := PChar(NewValue);
  CurrentLineIndex := LineNumber;
  fEditor.TokenLines.CropLines(CurrentLineIndex); <----can be nil here
  if fEditor <> nil then
  begin
    //0.3
    fEditor.TokenLines[CurrentLineIndex].Clear;
    if LineNumber < fEditor.LineIndex_Syntax then
      fEditor.LineIndex_Syntax := LineNumber;
  end;
  PosEnd := 0;
  Next;
end;
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Baffled by an "if" that doesn't work
« Reply #10 on: August 30, 2014, 10:53:23 pm »
If you're still getting problems I suggest you put the code from CheckIndex back into Remove() and RemoveFrom(), and comment CheckIndex out; and then the debugger will show you values from the stack of the actual called routine where it will be easier to pinpoint why the "if" seems to misbehave (if it indeed does) in the SynEdit context where you are using it, context outsiders can't easily reproduce exactly.

Henppy

  • Jr. Member
  • **
  • Posts: 60
Re: Baffled by an "if" that doesn't work
« Reply #11 on: August 30, 2014, 11:57:11 pm »
Ok, so here's what I think is happening. I inserted a ShowMessage and guess what, nothing showed up. I Run > Clean up Build files, run again an the dialog box showed up. Went back, removed the line, saved, run again... the dialog box showed up again.

So apparently Lazarus or FP are not re compiling the source even if it was modified.

Even more strange, I go and introduce an error, Ctrl-F9 an the thing complains. Good. Delete the error, compile, run, dialog box still showing up even when it's not in the code!

I don't remember tinkering with the settings, but could it be some setting?

EDIT: Thanks Never! Quite blunt mistake on my part.
« Last Edit: August 30, 2014, 11:59:00 pm by Henppy »

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Baffled by an "if" that doesn't work
« Reply #12 on: August 31, 2014, 12:43:45 am »
Checked your project settings
under project options/ mischellaneous the setting [Always build even if nothing is changed is not set]
maybe this will make a dif for you

« Last Edit: August 31, 2014, 12:46:48 am by Never »
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Baffled by an "if" that doesn't work
« Reply #13 on: August 31, 2014, 12:08:07 pm »
Quote
So apparently Lazarus or FP are not re compiling the source even if it was modified.

Even more strange, I go and introduce an error, Ctrl-F9 an the thing complains. Good. Delete the error, compile, run, dialog box still showing up even when it's not in the code!

I experienced this too. See my mail on ML: http://lists.lazarus.freepascal.org/pipermail/lazarus/2014-July/088017.html

Yesterday, I thought it's OK (I downloaded the latest trunk) but it happened again.

I was not able to create a simple demo for bugreport, it happens with larger projects only.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Baffled by an "if" that doesn't work
« Reply #14 on: August 31, 2014, 04:12:45 pm »
I reported it. Lazarus sometimes ignores changes and run old version of executable.

http://bugs.freepascal.org/view.php?id=26659
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018