Recent

Author Topic: Program crashing with exception during IO despite $I-  (Read 1054 times)

pleumann

  • Full Member
  • ***
  • Posts: 103
Program crashing with exception during IO despite $I-
« on: March 19, 2025, 12:41:51 pm »
Hello,

I have a (relatively large) program that needs to restrict itself to units available in classic Turbo Pascal (i.e. no SysUtils, no exceptions). I built a small function for getting the size of a file, or -1 if the file does not exist (see below) based on $I- and IOResult, just like we did several decades ago. The function works fine in isolation (the demo program below), but in the larger program I get an exception when `Reset` is called and the file does not exist.

Code: [Select]
An unhandled exception occurred at $00000001009C05D8:
EAccessViolation: Access violation
  $00000001009C05D8
  $00000001009D6498


I find it weird that I get an exception at all, because SysUtils is not used. I also don't understand why it fails in the larger program, but not in small demo case. The program header is the same, i.e. {$mode delphi} and the units are exactly what I use in both cases.

Code: Pascal  [Select][+][-]
  1. program FS;
  2.  
  3.  
  4. uses
  5.   Keyboard, Dos, Math, Process;
  6.  
  7. function FSize(Name: String): Integer;
  8. var
  9.   F: File;
  10. begin
  11.   {$i-}
  12.   Assign(F, Name);
  13.   Reset(F);
  14.   if IOResult = 0 then
  15.   begin
  16.     FSize := FileSize(F);
  17.     Close(F);
  18.   end
  19.   else FSize := -1;
  20.   {$i+}
  21. end;
  22.  
  23. begin
  24.   WriteLn('Size is ', FSize(ParamStr(1)), ' bytes.');
  25. end.
  26.  

Does anyone have an idea? I am on a Mac with M3 and Sequoia. FPC is 3.2.2.

cdbc

  • Hero Member
  • *****
  • Posts: 2062
    • http://www.cdbc.dk
Re: Program crashing with exception during IO despite $I-
« Reply #1 on: March 19, 2025, 12:47:21 pm »
Hi
The program does not use 'SysUtils', BUT does any of the other units used in your program, reference 'SysUtils'?!?
Maybe 'Math' or 'Process'???
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

pleumann

  • Full Member
  • ***
  • Posts: 103
Re: Program crashing with exception during IO despite $I-
« Reply #2 on: March 19, 2025, 12:52:54 pm »
Hi
The program does not use 'SysUtils', BUT does any of the other units used in your program, reference 'SysUtils'?!?
Maybe 'Math' or 'Process'???
Regards Benny

Hi Benny,

according to the online docs, I think they don't. Maybe I will check the RTL source code later. But the demo program would have to have the same problem then, because it uses exactly the same units as the real program. So I think that can't be the reason.

Regards
Joerg

rvk

  • Hero Member
  • *****
  • Posts: 6703
Re: Program crashing with exception during IO despite $I-
« Reply #3 on: March 19, 2025, 01:34:01 pm »
I also don't understand why it fails in the larger program, but not in small demo case.
That's because the problem is not in the code which is in the small demo program.
Your problem is somewhere else in the larger program.
Access Violation usually means you are accessing some memory which you don't have access to...
probably using an object or pointer with nil.

PS. I would move the readout of IOResult outside the {$I-} / {$I+} block, like is done in the manual: https://www.freepascal.org/docs-html/rtl/system/ioresult.html

PPS. The unit process and Math both contain the SysUtils in its use-clause (so exception handling is normal).

pleumann

  • Full Member
  • ***
  • Posts: 103
Re: Program crashing with exception during IO despite $I-
« Reply #4 on: March 19, 2025, 01:57:20 pm »
Moving the IOResult read out of the $I- block does not change anything. And I can pinpoint the access violation to exactly the Reset call in my function. Fun fact: If I remove the $I- it goes away and a get a normal EInOutError.

But thanks for the info that the other units are using SysUtils implicitly. I will see what I can do about that. My guess is that exceptions and $I- don't work together, it's either one or the other.

rvk

  • Hero Member
  • *****
  • Posts: 6703
Re: Program crashing with exception during IO despite $I-
« Reply #5 on: March 19, 2025, 02:35:59 pm »
My guess is that exceptions and $I- don't work together, it's either one or the other.
Mixing them should be possible.

What happens if you change the Assign(F... to AssignFile(F...

(They should be the same but if you have a large program where the Assign from memory assign is used, then it could go wrong.)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12142
  • FPC developer.
Re: Program crashing with exception during IO despite $I-
« Reply #6 on: March 19, 2025, 03:52:47 pm »
Things I quickly notice:

  • use a reset(f,1) for an untyped file
  • unit process uses classes, so probably also sysutils

jamie

  • Hero Member
  • *****
  • Posts: 6867
Re: Program crashing with exception during IO despite $I-
« Reply #7 on: March 19, 2025, 11:20:14 pm »
I always d
Code: Pascal  [Select][+][-]
  1. Inoutres :=0;
  2.  

Before pascal io
The only true wisdom is knowing you know nothing

pleumann

  • Full Member
  • ***
  • Posts: 103
Re: Program crashing with exception during IO despite $I-
« Reply #8 on: March 21, 2025, 11:18:16 am »
Hi,

I'm afraid none of the suggestions made a difference.

- It only happens in the large program, not in the isolated sample program (but the used units are the same).
- It only happens for a non-existing file.
- The crash happens inside the Reset() call (shown by adding WriteLn statements before and after).
- In my opinion, the larger program does nothing that would explain an access violation (but I may be wrong).

Maybe I will add some logging output to the RTL units to see where exactly the crash happens. Or I will try it with the Lazarus debugger.

Best regards
Joerg

rvk

  • Hero Member
  • *****
  • Posts: 6703
Re: Program crashing with exception during IO despite $I-
« Reply #9 on: March 21, 2025, 11:28:09 am »
- The crash happens inside the Reset() call (shown by adding WriteLn statements before and after).
- In my opinion, the larger program does nothing that would explain an access violation (but I may be wrong).
You are definitely wrong there. Otherwise you small program would crash too or your large program would run fine  :D

Keep in mind that the crash happens in Reset() but the cause could be much earlier. It could just "get out" during the Reset() because on that point, any lingering 'hanging' exceptions, could come out.


pleumann

  • Full Member
  • ***
  • Posts: 103
Re: Program crashing with exception during IO despite $I-
« Reply #10 on: March 21, 2025, 11:36:31 am »
I agree. Experience suggests that the problem usually sits in front of the keyboard. :)

But I really don't see it. I can make the routine independent of arguments, just local variables, and it will cause the crash. So there are no undefined pointers or other stuff I might pass into the routine as arguments. And I've never heard of "hanging" exceptions. When they are thrown they are either caught or passed on. They wouldn't materialize 30 seconds later.

But I'll keep on digging...

pleumann

  • Full Member
  • ***
  • Posts: 103
Re: Program crashing with exception during IO despite $I-
« Reply #11 on: March 21, 2025, 12:40:27 pm »
Found it (using the Lazarus debugger)! Let's not discuss it any further.  :-[

(Well, it was actually in a seemingly harmless routine that does my error handling. Nothing fancy, no exceptions, just a couple of WriteLns to dump stuff. But a recent change introduced a special case where a pointer could be nil, and a non-existing file could trigger -- or rather expose -- that. The result looked like a crash in Reset, but had I looked closely enough I would have noticed what really happened.)

Thanks for the comments & a good weekend everyone!
Joerg
« Last Edit: March 21, 2025, 12:49:14 pm by pleumann »

rvk

  • Hero Member
  • *****
  • Posts: 6703
Re: Program crashing with exception during IO despite $I-
« Reply #12 on: March 21, 2025, 01:43:23 pm »
Let's not discuss it any further.  :-[
We've all been there  ;) :D

 

TinyPortal © 2005-2018