Recent

Author Topic: IOResult error 103 in GUI program and not in console one  (Read 2258 times)

ddev

  • New Member
  • *
  • Posts: 17
IOResult error 103 in GUI program and not in console one
« on: August 04, 2020, 07:44:39 pm »
I have a console program I recently ported to Lazarus LCL. This program basically reads a command file, simulates a population process and writes results in text files.

Everything was working well until one week ago when I started having problems opening files for writing. The code I use is very standard:

Code: Pascal  [Select][+][-]
  1. f: TextFile;
  2. {$I-}
  3. assignFile (f, fileName);
  4. rewrite (f);
  5. {$I+}
  6. if IOResult <> 0 then
  7.    reportError;
  8.  

My problem is the following:

When I run the program with Lazarus LCL, as a GUI application, I can no longer open files for writing and I receive error 103 (This is new. Everything was working fine one week ago).

When I run the program as a console application, IOResult returns 0.

This is with the same output directory, or using other directories.

No problem either when I run the console app from XCode (I use a Mac).

The same problem exists when I compile and run the GUI application in Windows (Virtual PC) writing files in the Windows file system (so it is not a problem with the Mac file system).

On the Mac I use Lazarus 2.0.10 and FPC 3.2.0 and with Windows it's Lazarus 2.0.6 and FPC 3.0.4

Any idea on what is causing this??

Many thanks in advance,

Daniel
Lazarus 2.0.10
FPC 3.2.0
Mac OS X 10.14.6

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: IOResult error 103 in GUI program and not in console one
« Reply #1 on: August 04, 2020, 08:16:45 pm »
Can you provide a compile-able example and sample data that showing the issue?

Create a new folder, copy and paste all the necessary files except: the binary (exe file), *.bak, lib and backup folders. Compress the folder and send the zip here.

Or maybe you can try some demos here:
https://forum.lazarus.freepascal.org/index.php/topic,37766.msg254800.html#msg254800
https://forum.lazarus.freepascal.org/index.php/topic,39769.msg274036.html#msg274036

If both the demos run and produce the issue you mentioned, it should be something wrong with the system, maybe virus/antivirus, or your Lazarus.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: IOResult error 103 in GUI program and not in console one
« Reply #2 on: August 04, 2020, 09:19:50 pm »
An error 103 is strange (for a Rewrite) but another possible reason is that the GUI program is trying to create the file in a folder where it has no write permission, while the console one tries to do it in a "normal" one.

You can test this by using, for example:
Code: Pascal  [Select][+][-]
  1. ShowMessage(CleanAndExpandFilename(filename));
in your GUI version and
Code: Pascal  [Select][+][-]
  1. WriteLn(CleanAndExpandFilename(filename));
in the console one.

It's difficult to say more without seeing a more complete piece of code; for example, where is filename set? Is it an absolute or a relative name? etc.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: IOResult error 103 in GUI program and not in console one
« Reply #3 on: August 04, 2020, 09:48:55 pm »
I think newer versions of macOS require you to give a program file access permissions. When you are running a console application, you must give the Terminal app the rights (not the program that you are using in your console). Don't ask me why I think this is a cocoa thingy. Therefore it could be that your console application works because your Terminal got these rights, but your GUI application don't because this requires you to explicitely grant these rights.

That said, I haven't used Lazarus on macOS for a long time, and this is all speculation

ddev

  • New Member
  • *
  • Posts: 17
Re: IOResult error 103 in GUI program and not in console one
« Reply #4 on: August 04, 2020, 10:45:48 pm »
I solved my problem!

Before opening my textfile for writing, I was closing a file that had not been previously opened.

Don't ask me why this was a problem for the GUI app and not the console one :o
Lazarus 2.0.10
FPC 3.2.0
Mac OS X 10.14.6

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: IOResult error 103 in GUI program and not in console one
« Reply #5 on: August 04, 2020, 10:57:09 pm »
ah, that's because you had a dangling IoResults still active..

This is what I do to ensure I have a clean start …

InOutRes := 0;

that will clear any remaining unknow errors you may have forgotten to clean up

or you could just do  IORESULT;

Which will also clear it.
The only true wisdom is knowing you know nothing

ddev

  • New Member
  • *
  • Posts: 17
Re: IOResult error 103 in GUI program and not in console one
« Reply #6 on: August 05, 2020, 12:55:09 am »
Quote
or you could just do  IORESULT;

Which will also clear it.

Thanks. I have just added that line before all my assignFiles!
Lazarus 2.0.10
FPC 3.2.0
Mac OS X 10.14.6

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: IOResult error 103 in GUI program and not in console one
« Reply #7 on: August 05, 2020, 03:45:43 am »
You haven't tell me do those demos also cause the 103 error or not.

that's because you had a dangling IoResults still active..

I briefly searched the web for file reading/writing tutorials. None of them suggest we should closed the file first nor empty the ioresult right before opening a text file. I still believe there is something not correct in the OP's code, unfortunately he is too shy to show us the code.
« Last Edit: August 05, 2020, 04:53:31 am by Handoko »

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: IOResult error 103 in GUI program and not in console one
« Reply #8 on: August 05, 2020, 08:25:52 am »
On Windows you can try adding {$apptype console} instead of {$apptype gui} at the top of your main project file (not the main unit, but the lpr)
That will create a GUI app with a visible console. If that works, you can subsequently hide - not destroy - the console in the lpr (at the top) and everything should be fine. The GUI part still works as expected.

Another option is to redirect stdin, stdout and stderr directly to your GUI application.
« Last Edit: August 05, 2020, 08:28:21 am by Thaddy »
Specialize a type, not a var.

ddev

  • New Member
  • *
  • Posts: 17
Re: IOResult error 103 in GUI program and not in console one
« Reply #9 on: August 06, 2020, 10:44:06 pm »
You haven't tell me do those demos also cause the 103 error or not.

that's because you had a dangling IoResults still active..

I briefly searched the web for file reading/writing tutorials. None of them suggest we should closed the file first nor empty the ioresult right before opening a text file. I still believe there is something not correct in the OP's code, unfortunately he is too shy to show us the code.

This procedure shows the problem:

Code: Pascal  [Select][+][-]
  1. procedure danglingIOResult;
  2. var
  3.   f: TextFile;
  4.   i: longint;
  5. begin
  6. {$I-}
  7. closeFile(f);
  8. assign(f, 'toto.txt');
  9. rewrite(f);
  10. {$I+}
  11. i := IOResult;
  12. if i <> 0 then
  13.    writeLn ('error:', i)
  14. else
  15.    writeLn ('no error');
  16. end;

IOResult returns 103 even if the error is related to closeFile(f) and not to assignFile(f); Also 'toto.txt' is not open and a write access to it will return an exception error.

Contrarily to what I experimented in my app, the 103 error code is returned by IOResult both in a GUI and a console demo app.

As suggested by Jamie, the solution is simply to add a 'IOResult;' after 'closeFile(f);' to clean it up.
Lazarus 2.0.10
FPC 3.2.0
Mac OS X 10.14.6

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: IOResult error 103 in GUI program and not in console one
« Reply #10 on: August 07, 2020, 04:23:43 am »
Thank you for providing the code.

Based on it I wrote a demo, which can be downloaded on the link below. But I can't reproduce the issue. I can understand jamie's suggestion about clearing the ioresult. But as far as I know, it is not needed if the code is working good. It is only needed when the program try to recover from previous (unexpected) error. Correct me if I'm wrong.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Label1: TLabel;
  17.     procedure Button1Click(Sender: TObject);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. { TForm1 }
  28.  
  29. procedure TForm1.Button1Click(Sender: TObject);
  30. const
  31.   FileName = 'toto.txt';
  32. var
  33.   f: TextFile;
  34.   i: LongInt;
  35. begin
  36. {$I-}
  37. // closeFile(f);
  38.   AssignFile(f, FileName);
  39.   Rewrite(f);
  40. {$I+}
  41.   i := IOResult;
  42.   if i <> 0 then
  43.      Label1.Caption := 'Error: ' + i.ToString
  44.   else
  45.      Label1.Caption := 'No error';
  46. end;
  47.  
  48. end.

I tested it on Lazarus 2.0.10 Linux 64-bit. Anyone please download, test it and report back the result.

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: IOResult error 103 in GUI program and not in console one
« Reply #11 on: August 07, 2020, 05:34:32 am »
I ran it on Windows 10 (see sig), and generated a 64-bit executable.

The first button press leads to "No Error"  but pressing it again leads to "Error 5"

Each new run of the executable behaves the same way.  "No Error" for the first press of the button, but "Error 5" for all subsequent presses.
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: IOResult error 103 in GUI program and not in console one
« Reply #12 on: August 07, 2020, 05:58:32 am »
Thanks ASBzone for testing it.

Oh, I know now. The code has a serious bug. It does not close the file.
I think the puzzle is solved.

ASBzone, can you please test it again but adding a CloseFile command on the line where it should be?

 

TinyPortal © 2005-2018