Recent

Author Topic: Invalid Floating Point Operation when changing Directory in FileListBox  (Read 1323 times)

Flight714

  • New Member
  • *
  • Posts: 13
Hi all,

I'm having some good results with my new application so far, except that I can't get this to work...

The idea is that double-clicking on a directory item in a FileListBox will change to that directory. However, I'm getting an Invalid Floating Point Operation error when doing so.

Here is the relevant code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileListBox1DblClick(Sender: TObject);
  2. { Double click to change Directory }
  3. begin
  4.   ShowMessage( 'About to change directory to ' + ExtractFilePath(FileListBox1.Filename) );
  5.   if DirectoryExists( ExtractFilePath(FileListBox1.Filename) ) then
  6.   begin
  7.     { Invalid Floating Point Operation: }
  8.     FileListBox1.Directory := FileListBox1.Filename;
  9.   end;
  10. end;
  11.  

Can anyone offer tips on troubleshooting? I'm not even sure if I'm changing directories properly, but it seemed like this kind of approach matched the "FP Style" from what I could tell :D

I'm using Xubuntu 18.04 x64, Lazarus 1.8.2 / FPC 3.0.4 from the standard repositories. Thanks for any assistance.

winni

  • Hero Member
  • *****
  • Posts: 2119
Hi!

You should not read the items of the filelistbox ,while you are about to change it's contents.
So use a local  string variable and then

Code: Pascal  [Select][+][-]
  1. NewDir := ExtractFilePath(FileListBox1.Filename);

And then continue working with NewDir.

And second you should give the filelistbox time to read in the new directory. After

Code: Pascal  [Select][+][-]
  1.  FileListBox1.Directory := NewDir;

you should insert

Code: Pascal  [Select][+][-]
  1. Application.ProcessMessages;


Winni

Flight714

  • New Member
  • *
  • Posts: 13
Thanks winni. Have I interpreted your help correctly?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileListBox1DblClick(Sender: TObject);
  2. { Double click to change Directory }
  3. var
  4.   ChosenDirectory: String;
  5. begin
  6.   ShowMessage( 'About to change directory to ' + ExtractFilePath(FileListBox1.Filename) );
  7.   ChosenDirectory := ExtractFilePath(FileListBox1.Filename);
  8.   if DirectoryExists( ChosenDirectory ) then
  9.   begin
  10.     FileListBox1.Directory := ChosenDirectory;
  11.     Application.ProcessMessages;
  12.   end;
  13. end;

Now I am getting a Division by Zero error, unfortunately.

lucamar

  • Hero Member
  • *****
  • Posts: 3440
Try this instead:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileListBox1DblClick(Sender: TObject);
  2. { Double click to change Directory }
  3. var
  4.   NewDir: string;
  5. begin
  6.   NewDir := FileListBox1.Filename;
  7.   if (NewDir <> '') and DirectoryExists(NewDir) then begin
  8.     ShowMessage( 'Changing directory to ' + NewDir);
  9.     FileListBox1.Directory := NewDir;
  10.   end;
  11. end;

Despite what winni said, it shouldn't matter if you delete NewDir and use FileListBox1.Filename everywhere: it is read before Directory is set. Using a new var is just a convenience and a guard against future changes where it might matter.

What is really a NO-NO-NO! in this case is calling ProcessMessages. There is no need for that and it may do lots of harm, since we are in an event which is itself almost directly triggered by a system message. It can be done (if absolutely needed) and will probably work almost always, but better safe than sorry.

If you realy wish to "wait" until the list is updated just call FilesBox1.UpdateFileList;, although it shouldn't be necessary.
« Last Edit: July 27, 2019, 03:17:52 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Flight714

  • New Member
  • *
  • Posts: 13
Thank you lucamar. This feels like progress! Here is what is happening now, substituting the code you so kindly provided. When I double-click a directory, I get a "Division by Zero" error BUT the directory changes after OK is clicked.

So, it works but with a little interruption :-) Any ideas on why the Division by Zero is happening?

lucamar

  • Hero Member
  • *****
  • Posts: 3440
You're doing nothing else? Where does the exception occur?

There's nothing in that code that should cause any exception. Can you share the full project?

ETA Just for testing, I'm attaching the small demo I did the 25th for your other post. Tell me if it also gives you any exception.

EATA: Regarding this:

BUT the directory changes after OK is clicked.

In your previous code it wasn't changing because ExtractFilePath eliminated the new directory from the string. So it was probably working too, only no change was seen :)
« Last Edit: July 27, 2019, 04:33:55 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Flight714

  • New Member
  • *
  • Posts: 13
Great, I will test that out. Here is my archive. Thank you for taking a look.

Edit: I do not get the same error when changing directories using your project.
« Last Edit: July 27, 2019, 04:38:20 am by Flight714 »

lucamar

  • Hero Member
  • *****
  • Posts: 3440
Got it; I'll check it in a few minutes.

I see we are both of the "literal" school of project naming :D

ETA: Got it!

The problems is in the OnChange and OnClick event handlers. Each time you use:
Code: Pascal  [Select][+][-]
  1. FileListBox1.Items[FileListBox1.ItemIndex]
you should first make sure ItemIndex is valid, i.e.
Code: Pascal  [Select][+][-]
  1. if (FileListBox1.ItemIndex >= 0) and (FileListBox1.ItemIndex < FileListBox1.Count) then
  2.   {do something}

Either that or use FileListBox1.Filename instead.

Yeah, checked and tested: with the guard in place the exceptions disappear :)
« Last Edit: July 27, 2019, 05:28:29 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Flight714

  • New Member
  • *
  • Posts: 13

I see we are both of the "literal" school of project naming :D

Haha, for sure  :D

The problems is in the OnChange and OnClick event handlers. Each time you use:
Code: Pascal  [Select][+][-]
  1. FileListBox1.Items[FileListBox1.ItemIndex]
you should first make sure ItemIndex is valid, i.e.
Code: Pascal  [Select][+][-]
  1. if (FileListBox1.ItemIndex >= 0) and (FileListBox1.ItemIndex < FileListBox1.Count) then
  2.   {do something}

Either that or use FileListBox1.Filename instead.

Yeah, checked and tested: with the guard in place the exceptions disappear :)

That's great! Thank you so much for your help. I had to step out of the office but I will try it tomorrow.


lucamar

  • Hero Member
  • *****
  • Posts: 3440
See this post: https://forum.lazarus.freepascal.org/index.php/topic,9280.msg49731.html#msg49731

Good catch but I don't think that's the problem. And after all that post is nine years old; that's a lot of time for an uncorrected bug (except when it's a MS one, of course ;) )
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 7957
The original code in the first post is correct. It does not crash in my tests on Windows (Laz trunk / fpc 3.04, 32 bit)) and on Ubuntu 18.0.4 (Laz trunk / fpc 3.0.4, 64 bit).

Maybe this bug has been fixed after the release of your Laz 1.8.2? Can you try Laz 2.0.2 (or wait a short time, Laz 2.0.4 will be released shortly)?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

lucamar

  • Hero Member
  • *****
  • Posts: 3440
The crash is not in that code, it's on a couple other events which are triggered by a mouse-click or changes in the FilesListBox items or in the selected one's index.

In those events he was accessing the Items of the FileListBox with the list's own ItemIndex without checking first whether it actually pointed to an Item: Result? An exception when it doesn't.

It's easy to see: download the code from this post, compile it as-is, without changes and run it. Then click in the FilesListBox, which will probably be empty because the path is hard-coded to a particular one in the OP's machine.

The BOOM! is almost instantaneous :D

That said, it doesn't realy explains why it happened  also when clicking a real item. That is a little strange and I'll reserve some time to investigate a little more deeply.
« Last Edit: July 27, 2019, 04:45:07 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Flight714

  • New Member
  • *
  • Posts: 13
Very interesting! It works just fine when I use FileListBox1.Filename OR the other solution you provided, lucamar. That was very helpful and my code looks cleaner anyway, now that I am using FileListBox1.Filename. :)

 

TinyPortal © 2005-2018