SDL2: Runtime Error 216 on MacOS-arm lazarus / 39-Graphics ---------------------------------------------------------------------- mischi · 2025-01-12 21:33 Hi. In a minimal SDL2 program SDL_CreateWindow creates Runtime Error 216 on macOS-arm (macOS 15.2). The attachment is a minimal stripped down version, which still creates the error. Any modification I could imagine, including a complete declaration of PSDL_Window, did not change anything and I am at the end of wisdom. Any hints are welcome. Does the example run on other platforms? A corresponding example in C runs without problems. SDL2 is installed using MacPorts and resides in /opt/local. The command to compile the example is: Log: michael@Hal-9001 Pascal % fpc Test1.pas -Fl/opt/local/lib Free Pascal Compiler version 3.2.2 [2024/11/27] for aarch64 Copyright (c) 1993-2021 by Florian Klaempfl and others Target OS: Darwin for AArch64 Compiling Test1.pas Assembling test1 Linking Test1 ld: warning: ignoring duplicate libraries: '-lc' 45 lines compiled, 0.1 sec michael@Hal-9001 Pascal % ./Test1                                Runtime error 216 at $0000000189880F50   $0000000189880F50   $000000018987F334   $000000018987EEB4   $000000018987EB3C   $0000000189AD23F4   $0000000102806528   $00000001027EB66C   $0000000102460D08 ---------------------------------------------------------------------- TRon · 2025-01-13 00:56 Is there a particular reason to use your own defines library headers and not use the standard SDL2 units. Does the example code work with the standard headers ? Did you also install opengl ? ---------------------------------------------------------------------- Seenkao · 2025-01-13 01:33 Quote from: TRon on January 13, 2025, 01:56:39 amDid you also install opengl ? Разве надо на Mac надо включать OpenGL? На нём вроде по умолчанию идёт. Google translate: Do you really need to enable OpenGL on a Mac? It seems to work by default. mischi, включите отладочную информацию в проекте, тогда может более точную ошибку увидите. Google translate: mischi, enable debug information in the project, then maybe you will see a more precise error. ---------------------------------------------------------------------- TRon · 2025-01-13 01:52 Quote from: Seenkao on January 13, 2025, 02:33:07 amDo you really need to enable OpenGL on a Mac? It seems to work by default. I have no idea what opengl library version SDL expects or what OpenGL auxiliary libraries SDL depends on for a mac. You might be perfectly right that this does not matter for a mac but, I honestly don't know. @mischi: Besides the request from Seenkao to enable debug (which can be a bit tricky for a mac: requires external debug symbols and a particular debug format) some output along the way might be able to help as well. Right now there is no hint whatsoever at what function exactly the code fails. ---------------------------------------------------------------------- mischi · 2025-01-13 09:19 Quote from: TRon on January 13, 2025, 01:56:39 amIs there a particular reason to use your own defines library headers and not use the standard SDL2 units. Does the example code work with the standard headers ? Did you also install opengl ? The error showed up using the standard SDL2 units. The only reason for having my own library headers is to reduce the level of complexity of the example. OpenGL is part of macOS as Seenkao mentioned. ---------------------------------------------------------------------- mischi · 2025-01-13 09:25 Quote from: TRon on January 13, 2025, 02:52:21 amQuote from: Seenkao on January 13, 2025, 02:33:07 amDo you really need to enable OpenGL on a Mac? It seems to work by default. I have no idea what opengl library version SDL expects or what OpenGL auxiliary libraries SDL depends on for a mac. You might be perfectly right that this does not matter for a mac but, I honestly don't know. @mischi: Besides the request from Seenkao to enable debug (which can be a bit tricky for a mac: requires external debug symbols and a particular debug format) some output along the way might be able to help as well. Right now there is no hint whatsoever at what function exactly the code fails. Log with debug switched on: michael@Hal-9001 Pascal % fpc -gw2 -gl Test1.pas -Fl/opt/local/lib Free Pascal Compiler version 3.2.2 [2024/11/27] for aarch64 Copyright (c) 1993-2021 by Florian Klaempfl and others Target OS: Darwin for AArch64 Compiling Test1.pas Assembling test1 Linking Test1 ld: warning: ignoring duplicate libraries: '-lc' 45 lines compiled, 0.3 sec michael@Hal-9001 Pascal % lldb Test1 (lldb) target create "Test1" Current executable set to '/Users/michael/Developer/SDL2 Trial/Pascal/Test1' (arm64). (lldb) run Process 7693 launched: '/Users/michael/Developer/SDL2 Trial/Pascal/Test1' (arm64) 2025-01-13 10:16:48.226151+0100 Test1[7693:22258102] [plugin] AddInstanceForFactory: No factory registered for id F8BB1C28-BAE8-11D6-9C31-00039315CD46 Process 7693 stopped * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=1, subcode=0x1e683920)     frame #0: 0x0000000189880f50 AppKit`_NSGetCGFloatAppConfig + 216 AppKit`_NSGetCGFloatAppConfig: ->  0x189880f50 <+216>: fsub   d0, d9, d8     0x189880f54 <+220>: fneg   d1, d0     0x189880f58 <+224>: fcmp   d0, #0.0     0x189880f5c <+228>: fcsel  d10, d1, d0, mi Target 0: (Test1) stopped. (lldb) bt * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=1, subcode=0x1e683920)   * frame #0: 0x0000000189880f50 AppKit`_NSGetCGFloatAppConfig + 216     frame #1: 0x000000018a3612fc AppKit`NSWindowResolvedScaleFactor + 484     frame #2: 0x000000018987f334 AppKit`-[NSWindow _commonInitFrame:styleMask:backing:defer:] + 288     frame #3: 0x000000018987eeb4 AppKit`-[NSWindow _initContent:styleMask:backing:defer:contentView:] + 876     frame #4: 0x000000018987eb3c AppKit`-[NSWindow initWithContentRect:styleMask:backing:defer:] + 48     frame #5: 0x0000000189ad23f4 AppKit`-[NSWindow initWithContentRect:styleMask:backing:defer:screen:] + 24     frame #6: 0x00000001004ce528 libSDL2-2.0.0.dylib`Cocoa_CreateWindow + 528     frame #7: 0x00000001004b366c libSDL2-2.0.0.dylib`SDL_CreateWindow_REAL + 1200     frame #8: 0x0000000100000d0c Test1`PASCALMAIN at Test1.pas:37:13     frame #9: 0x000000010002c140 Test1`FPC_SysEntry + 32     frame #10: 0x0000000100000c10 Test1`FPC_SYSTEMMAIN + 80     frame #11: 0x0000000100000c68 Test1`main(ARGC=1, ARGV=0x000000016fdff5e8, ARGP=0x000000016fdff5f8) + 40     frame #12: 0x000000018589c274 dyld`start + 2840 ---------------------------------------------------------------------- Seenkao · 2025-01-13 09:45 Quote from: mischi on January 13, 2025, 10:25:05 amAppKit`_NSGetCGFloatAppConfig: ->  0x189880f50 <+216>: fsub   d0, d9, d8     0x189880f54 <+220>: fneg   d1, d0     0x189880f58 <+224>: fcmp   d0, #0.0     0x189880f5c <+228>: fcsel  d10, d1, d0, mi this is what i found https://github.com/DynamoRIO/dynamorio/issues/5023 mischi, попробуйте другим способом использовать OpenGL (с другими библиотеками), возможно в них не будет данной ошибки. Google translate: mischi, try using OpenGL in a different way (with other libraries), perhaps they will not have this error. ---------------------------------------------------------------------- mischi · 2025-01-13 10:12 It would be nice, if someone could run the example on macOS-x86_64. ---------------------------------------------------------------------- Seenkao · 2025-01-13 10:24 Quote from: mischi on January 13, 2025, 11:12:46 amIt would be nice, if someone could run the example on macOS-x86_64. мои демонстрации работают на Mac Ventura с использованием OpenGL. Но на ARM (aarch64) я их не проверял... Я использую ZenGL. (я ещё не собрал необходимые библиотеки для ARM, пока не могу понять как это сделать на x86-64 машине). Google translate: my demos work on Mac Ventura using OpenGL. But I haven't tested them on ARM (aarch64)... I use ZenGL. (I haven't yet compiled the necessary libraries for ARM, and I can't figure out how to do this on an x86-64 machine). ---------------------------------------------------------------------- mischi · 2025-01-13 13:11 I do not think that OpenGL is a problem. 1) Nothing OpenGL related shows up in the backtrace. 2) I have a demo written in C doing the same and some more (Show a png image in the window) and that works fine. Can you run my example on your Mac? Either as it is or with standard SDL2. ---------------------------------------------------------------------- mischi · 2025-01-14 19:05 Update: I tried basically the same from a Fortran program using gfortran. This works. Is this a hint that fpc is doing something unwanted? MiSchi. ---------------------------------------------------------------------- fcu · 2025-01-15 08:48 can you try this : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Program Test1;uses math;const  {$IFDEF WINDOWS}    SDL_LibName = 'SDL2.dll';  {$ENDIF}  {$IFDEF UNIX}    {$IFDEF DARWIN}      SDL_LibName = 'libSDL2.dylib';      {$linklib libSDL2}      {$PASCALMAINNAME SDL_main}        {$ELSE}          SDL_LibName = 'libSDL2.so';    {$ENDIF}  {$ENDIF}  SDL_INIT_VIDEO           = $00000020;  SDL_WINDOWPOS_CENTERED   = $2FFF0000;  SDL_WINDOW_OPENGL        = 2;type  SInt32 = Int32;  PSDL_Window = Pointer;var  window: PSDL_Window;  OldMask, NewMask: TFPUExceptionMask; function SDL_Init(flags: UInt32): SInt32 cdecl; external SDL_LibName;function SDL_GetError: pchar cdecl; external SDL_LibName;function SDL_CreateWindow(title: pchar; x, y, w, h: SInt32; flags: Uint32): PSDL_Window cdecl; external SDL_LibName;procedure SDL_DestroyWindow(window: PSDL_Window) cdecl; external SDL_LibName;procedure SDL_Quit cdecl; external SDL_LibName; begin OldMask := Math.GetExceptionMask(); NewMask := OldMask; Include(NewMask, exInvalidOp); Include(NewMask, exZeroDivide); Math.SetExceptionMask(NewMask);   if (SDL_Init(SDL_INIT_VIDEO) < 0) then  begin     writeln ('Error SDL2 Initialization: ', SDL_GetError);  end;    window := SDL_CreateWindow('First program', SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);  if (window = nil) then  begin     writeln ('Error window creation: ', SDL_GetError);  end;   Math.SetExceptionMask(OldMask);   SDL_DestroyWindow(window);  SDL_Quit;end. ---------------------------------------------------------------------- mischi · 2025-01-15 10:31 Tried: michael@Hal-9001 Pascal % ./Test1 An unhandled exception occurred at $000000010EAF1480: EAccessViolation: Access violation   $000000010EAF1480   $0000000190C4592C   $0000000190C45390   $0000000185A695B4   $0000000185A78E08   $0000000190C45480   $0000000190C45C14   $0000000190AB6820   $000000018A5C9DB0   $000000018A5C84A8   $000000018A5C8208   $000000018A5C7C44   $000000018A3C91A8   $000000018995BD44   $000000018987F618   $000000018987EEB4   $000000018987EB3C Backtrace from debugger: (lldb) bt * thread #1, queue = 'com.Metal.DeviceDispatch', stop reason = EXC_BAD_INSTRUCTION (code=1, subcode=0x1e220800)   * frame #0: 0x000000010e45d480 AGXMetal13_3`AGX::SamplerStateEncoder Gen4::SamplerStateFields::SamplerStateFi elds(AGX::SamplerDescriptor const&) + 136     frame #1: 0x000000010e44ca78 AGXMetal13_3`-[AGXG13GFamilyDevice initWithAcceleratorPort:simultaneousInstances:] + 2796     frame #2: 0x000000010e4510f0 AGXMetal13_3`-[AGXG13GDevice initWithAcceleratorPort:] + 52     frame #3: 0x0000000190c4592c Metal`-[MTLIOAccelService initWithAcceleratorPort:deviceClass:] + 492     frame #4: 0x0000000190c45390 Metal`-[MTLIOAccelServiceGlobalContext registerService:deviceClass:] + 156     frame #5: 0x0000000185a695b4 libdispatch.dylib`_dispatch_client_callout + 20     frame #6: 0x0000000185a78e08 libdispatch.dylib`_dispatch_lane_barrier_sync_invoke_and_complete + 56     frame #7: 0x0000000190c45480 Metal`-[MTLIOAccelServiceGlobalContext processPendingCreateIOAccelServiceRequests] + 184     frame #8: 0x0000000190c45c14 Metal`MTLRegisterDevices + 116     frame #9: 0x0000000190ab6820 Metal`MTLCopyAllDevices + 24     frame #10: 0x000000018a5c9db0 AppKit`-[NSCGSWindow _createContext] + 344     frame #11: 0x000000018a5c84a8 AppKit`-[NSCGSWindow _createRootLayerAndContextIfNeededUsingAsyncBehavior:] + 584     frame #12: 0x000000018a5c8208 AppKit`-[NSCGSWindow initWithConnectionID:flags:] + 276     frame #13: 0x000000018a5c7c44 AppKit`+[NSCGSWindow(NSCGSWindowCreation) windowWithConnectionID:flags:] + 52     frame #14: 0x000000018a3c91a8 AppKit`_NXCreateWindow + 428     frame #15: 0x000000018995bd44 AppKit`-[NSWindow _commonAwake] + 672     frame #16: 0x000000018987f618 AppKit`-[NSWindow _commonInitFrame:styleMask:backing:defer:] + 1028     frame #17: 0x000000018987eeb4 AppKit`-[NSWindow _initContent:styleMask:backing:defer:contentView:] + 876     frame #18: 0x000000018987eb3c AppKit`-[NSWindow initWithContentRect:styleMask:backing:defer:] + 48     frame #19: 0x0000000189ad23f4 AppKit`-[NSWindow initWithContentRect:styleMask:backing:defer:screen:] + 24     frame #20: 0x000000010057a528 libSDL2-2.0.0.dylib`Cocoa_CreateWindow + 528     frame #21: 0x000000010055f66c libSDL2-2.0.0.dylib`SDL_CreateWindow_REAL + 1200     frame #22: 0x0000000100001dc0 Test1`PASCALMAIN at Test1.pas:46:13     frame #23: 0x000000010002d200 Test1`FPC_SysEntry + 32     frame #24: 0x0000000100001c30 Test1`FPC_SYSTEMMAIN + 80     frame #25: 0x0000000100001c88 Test1`main(ARGC=1, ARGV=0x000000016fdff5c8, ARGP=0x000000016fdff5d8) + 40     frame #26: 0x000000018589c274 dyld`start + 2840 ---------------------------------------------------------------------- mischi · 2025-01-15 11:13 After adding the exception "exOverflow", it worked. The exception "exZeroDivide" could then be removed. Thanks for this. Can the code remain like this or is there a way to fix this properly? Does this hint to a bug in libSDL2? ---------------------------------------------------------------------- fcu · 2025-01-15 16:32 i think its fpc bug , i run into this situation with win10 64bits & sdl2 & fpc , i did try with other languages on this system and every thing works fine except fpc ---------------------------------------------------------------------- Fred vS · 2025-01-15 18:04 Quote from: mischi on January 15, 2025, 12:13:23 pmAfter adding the exception "exOverflow", it worked. The exception "exZeroDivide" could then be removed. Thanks for this. Can the code remain like this or is there a way to fix this properly? Does this hint to a bug in libSDL2? I tested your code under Linux amd64 and it works. About exception, I suppose you know the trick: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ... uses math, ...... begin SetExceptionMask(GetExceptionMask + [exZeroDivide] + [exInvalidOp] +    [exDenormalized] + [exOverflow] + [exUnderflow] + [exPrecision]);... ---------------------------------------------------------------------- TRon · 2025-01-15 18:44 Quote from: fcu on January 15, 2025, 05:32:20 pmi think its fpc bug , i run into this situation with win10 64bits & sdl2 & fpc , i did try with other languages on this system and every thing works fine except fpc It is not exactly a bug but a feature, see wiki (check the link as well) fwiw: for gtk appliciations for aarch64 this was already reported and fixed in trunk.