Recent

Author Topic: Exception EFCreateError cannot be handled on MacOS M1  (Read 279 times)

d4eva

  • New Member
  • *
  • Posts: 16
Exception EFCreateError cannot be handled on MacOS M1
« on: September 18, 2022, 09:03:30 pm »
I'm getting very weird behavior of my app.
My app crashes when it tries to create a file with invalid file name.
The real code is a bit different, but it's similar to this:

Code: Pascal  [Select][+][-]
  1.  
  2. procedure Foo(const AFileName: string);
  3. var
  4.   f: TFileStream;
  5. begin
  6.   try
  7.     f := TFileStream.Create(AFileName, fmCreate);
  8.     f.Free;
  9.     if FileExists(AFileName) then DeleteFile(AFileName);
  10.   except
  11.     on E: Exception do begin
  12.       // the code never gets here
  13.       raise Exception.Create(Format('Unable to create output file %s', [AFileName]));
  14.     end;
  15.   end;
  16. end;
  17.  

Thing is that while in the IDE the EFCreateError is shown and after that I see the Disassembly window and then IDE freezes and app just crashes.

If I run it from the shell it gets terminated with the following error:

2022-09-18 21:23:45.262 ****[6525:46831] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSBigMutableString replaceCharactersInRange:withString:]: nil argument'
*** First throw call stack:
(
   0   CoreFoundation                      0x00007ff80f7d5e9b __exceptionPreprocess + 242
   1   libobjc.A.dylib                     0x00007ff80f537e48 objc_exception_throw + 48
   2   Foundation                          0x00007ff810504955 -[NSBigMutableString replaceCharactersInRange:withString:] + 1163
   3   Foundation                          0x00007ff8104fc0b1 -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 367
   4   UIFoundation                        0x00007ff812f3ea97 __NSConcreteTextStorageLockedForwarding + 66
   5   UIFoundation                        0x00007ff812f790f2 -[NSConcreteTextStorage replaceCharactersInRange:withString:] + 74
   6   AppKit                              0x00007ff8122d8a46 -[NSTextView _insertText:replacementRange:] + 1760



I tried to reproduce it with a small console app, but there it worked just fine.

Lazarus 2.2.2 (Intel mode)
Mac M1
macOS 12.6
FPC 3.3.1 (latest from the trunk)

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1019
Re: Exception EFCreateError cannot be handled on MacOS M1
« Reply #1 on: September 18, 2022, 09:06:02 pm »
That is an Objective-C exception. Those indeed cannot be caught in FPC currently. The same goes for C++ exceptions.

d4eva

  • New Member
  • *
  • Posts: 16
Re: Exception EFCreateError cannot be handled on MacOS M1
« Reply #2 on: September 18, 2022, 09:23:49 pm »
Huh, is there any workaround for this?

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1019
Re: Exception EFCreateError cannot be handled on MacOS M1
« Reply #3 on: September 18, 2022, 09:32:13 pm »
I think the issue happens when you try to display this invalid string in a GUI dialog/message box. In what way is your string invalid? Does it contain invalid utf-8 sequences?

https://developer.apple.com/documentation/foundation/nsmutablestring/1416524-replacecharactersinrange doesn't mention it can throw exceptions except for out-of-range ones though.

d4eva

  • New Member
  • *
  • Posts: 16
Re: Exception EFCreateError cannot be handled on MacOS M1
« Reply #4 on: September 18, 2022, 09:38:53 pm »
I think the issue happens when you try to display this invalid string in a GUI dialog/message box. In what way is your string invalid? Does it contain invalid utf-8 sequences?

https://developer.apple.com/documentation/foundation/nsmutablestring/1416524-replacecharactersinrange doesn't mention it can throw exceptions except for out-of-range ones though.

Yes, the file name contained some weird characters and displaying it was the problem. When I removed the file name from the error message, the program is not crashing anymore. I can live with that, thanks.

 

TinyPortal © 2005-2018