Search the Community
Showing results for tags 'spawning processes'.
Found 1 result
-
macOS process spawning: Exception class 6 when calling addObserver
Navid Madani posted a topic in Cross-platform
unit uMacOSSpawn; interface uses System.SysUtils , System.Classes , Macapi.Foundation , Macapi.ObjectiveC , Macapi.ObjCRuntime , Macapi.Helpers ; type TProcessSpawn = class(TOCLocal) private fTask: NSTask; fInputPipe: NSPipe; fOutputPipe: NSPipe; fErrorPipe: NSPipe; fNotify: TNotifyEvent; fOutput: string; function GetNotify: TNotifyEvent; procedure SetNotify(const Value: TNotifyEvent); function GetOutput: string; public constructor Create(FileName: PWideChar); destructor Destroy; override; procedure NotificationReceived(notification: NSNotification); cdecl; procedure Input(const aText: string); property Output: string read GetOutput; property Notify: TNotifyEvent read GetNotify write SetNotify; end; implementation { TProcessSpawn } constructor TProcessSpawn.Create(FileName: PWideChar); var FileHandleLocObj: ILocalObject; InputLocObj: ILocalObject; OutputLocObj: ILocalObject; ErrorLocObj: ILocalObject; begin inherited Create; fInputPipe := TNSPipe.Create; fOutputPipe := TNSPipe.Create; fErrorPipe := TNSPipe.Create; fTask := TNSTask.Wrap(TNSTask.Wrap(TNSTask.OCClass.alloc).init); fTask.setLaunchPath(StrToNSStr(FileName)); if Supports(fInputPipe, ILocalObject, InputLocObj) and Supports(fOutputPipe, ILocalObject, OutputLocObj) and Supports(fErrorPipe, ILocalObject, ErrorLocObj) and Supports(fOutputPipe.fileHandleForReading, ILocalObject, FileHandleLocObj) then begin fTask.setStandardInput(InputLocObj.GetObjectID); fTask.setStandardOutput(OutputLocObj.GetObjectID); fTask.setStandardError(ErrorLocObj.GetObjectID); fOutputPipe.fileHandleForReading.acceptConnectionInBackgroundAndNotify; fOutputPipe.fileHandleForReading.readInBackgroundAndNotify; TNSNotificationCenter.Wrap(TNSNotificationCenter.OCClass.defaultCenter).addObserver( Self.GetObjectID, sel_getUid('NotificationReceived:'), NSFileHandleReadCompletionNotification, FileHandleLocObj.GetObjectID ); fTask.launch; // fNSTask.waitUntilExit; end else raise Exception.Create('ILocalObject interface not supported.'); end; destructor TProcessSpawn.Destroy; begin fTask.release; fInputPipe.release; fOutputPipe.release; fErrorPipe.release; inherited; end; function TProcessSpawn.GetNotify: TNotifyEvent; begin Result := fNotify; end; function TProcessSpawn.GetOutput: string; begin Result := fOutput; end; procedure TProcessSpawn.NotificationReceived(notification: NSNotification); var data: NSData; str: NSString; notifHandle: NSFileHandle; begin notifHandle := TNSFileHandle.Wrap(notification.&object); if notifHandle = fOutputPipe.fileHandleForReading then begin data := fOutputPipe.fileHandleForReading.availableData; str := TNSString.Wrap(TNSString.Alloc.initWithData(data, NSUTF8StringEncoding)); try fOutput := NSStrToStr(str); if Assigned(fNotify) then fNotify(Self); finally str.release; end; end; end; procedure TProcessSpawn.Input(const aText: string); var data: NSData; str: NSString; begin str := StrToNSStr(aText + #10); data := str.dataUsingEncoding(NSUTF8StringEncoding); fInputPipe.fileHandleForWriting.writeData(data); end; procedure TProcessSpawn.SetNotify(const Value: TNotifyEvent); begin fNotify := Value; end; end. I am trying to create a unit that spawns a console process on macOS that receives text input through stdin, and returns the result through stdout. I am not an iOS programmer, but I think I managed to get things done correctly based on documentation I consulted, and the unit below compiles without errors or warnings. However, the .addObserver call in the constructor (line 67) causes a runtime exception: First chance exception at $00000001823C976C. Exception class 6. Process Project1 (2540). Is this my fault, or could this be a bug in Delphi's Objective-C bridge? Thanks to everyone in advance. P.S.: I should also mention that debugging the RTL using Delphi 12 Athens on my Apple Silicon device running macOS Sonoma and Windows 11 Pro for ARM virtualized with Parallels, hangs the IDE 😞- 3 replies
-
- macos
- spawning processes
-
(and 1 more)
Tagged with: