I have Cocoa code that build custom NSAppleScript events buy invoking subroutines in complied .scpt files which are included inside the Cocoa application. This code has worked for years.
Now this code is crashing with [NSApplicaton _crashOnException] that I can’t trap with a @try.
// to avoid linking issues
#ifndef kASSubroutineEvent
#define kASSubroutineEvent (FourCharCode)'psbr'
#endif
#ifndef kASAppleScriptSuite
#define kASAppleScriptSuite (FourCharCode)'ascr'
#endif
#ifndef keyASSubroutineName
#define keyASSubroutineName (FourCharCode)'snam'
#endif
…
NSAppleEventDescriptor *currentProcess = [NSAppleEventDescriptor currentProcessDescriptor] ;
NSAppleEventDescriptor *executeSubroutineEvent = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite eventID:kASSubroutineEvent targetDescriptor: currentProcess returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
NSAppleEventDescriptor *handlerName = [NSAppleEventDescriptor descriptorWithString:handler.lowercaseString];
[executeSubroutineEvent setParamDescriptor:handlerName forKeyword:keyASSubroutineName];
NSAppleEventDescriptor* list = [NSAppleEventDescriptor listDescriptor];
int i=0; va_list ppp; va_start(ppp, firstParam);
NSAppleEventDescriptor * aParam = firstParam;
while (aParam) {
[list insertDescriptor:aParam atIndex:++i];
aParam = va_arg(ppp, NSAppleEventDescriptor *);
}
[executeSubroutineEvent setParamDescriptor:list forKeyword:keyDirectObject];
errorDict = nil;
NSAppleEventDescriptor *eventResult = [compiledAppleScript executeAppleEvent:executeSubroutineEvent error:&errorDict];
if ((!eventResult.descriptorType) || (errorDict!=nil)) {
it crashed on the 'eventResult' line: EXC_BAD_INSTRUCTION
#0 0x00007fff90cca28e in -[NSApplication _crashOnException:] ()
#1 0x00007fff90d9034e in __37+[NSDisplayCycle currentDisplayCycle]_block_invoke.31 ()
#2 0x00007fff92cf3f67 in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#3 0x00007fff92cf3ed7 in __CFRunLoopDoObservers ()
#4 0x00007fff92cd4b79 in __CFRunLoopRun ()
#5 0x00007fff92cd45b4 in CFRunLoopRunSpecific ()
#6 0x00007fff92275f6c in RunCurrentEventLoopInMode ()
#7 0x00007fff923c2017 in GetNextEventMatchingMask ()
#8 0x00007fff923c1f15 in WNEInternal ()
#9 0x0000000104e83f9a in AEDefaultActiveProc(void*) ()
#10 0x0000000104e88955 in InternalComponentActive(unsigned int) ()
#11 0x0000000104ebd51b in UASRemoteSend(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char*) ()
#12 0x0000000104e93c6c in UASActor_Send(unsigned char, unsigned char, unsigned char) ()
#13 0x0000000104e9f521 in UASExecute1() ()
#14 0x0000000104e70d7e in ASExecuteEvent(AEDesc const*, unsigned int, int, unsigned int*) ()
#15 0x0000000104e692dc in AppleScriptComponent ()
#16 0x0000000104e839e5 in AGenericCall::Delegate(ComponentInstanceRecord*) ()
#17 0x0000000104e839ab in AGenericManager::HandleOSACall(ComponentParameters*) ()
#18 0x0000000104e82fac in GenericComponent ()
#19 0x00007fff925fca5f in OSAExecuteEvent ()
#20 0x00007fff947cd913 in -[NSAppleScript(NSPrivate) _executeAppleEvent:withMode:error:] ()