Hard crashes in NSAppleScriptMBS

Even for 64bit my users continue to get hard crashes in NSAppleScriptMBS:

[quote]Thread 3:
0 com.apple.AE 0x00007fff470809b5 OpaqueAEStreamRef::closeDesc() + 165
1 com.apple.applescript 0x00007fff56ec7cb2 AEStream_WriteKeyDesc + 12
2 com.apple.applescript 0x00007fff56ef203a UASStartObjectSpecifier(AEStream&, unsigned int) + 64
3 com.apple.applescript 0x00007fff56ef1b4a TUASProperty::WriteWithoutContainer(AEStream&) + 78
4 com.apple.applescript 0x00007fff56ef1f6c TUASProperty::WriteForTarget(AEStream&, unsigned char) + 14
5 com.apple.applescript 0x00007fff56f06ad0 TUASValue::BecomeAEDescLocation(AEDesc&, TUASApplication*, unsigned char) + 104
6 com.apple.applescript 0x00007fff56ef9853 UASBuildEvent(TStackFrame_UASRemoteSend*, unsigned char, AEDesc*) + 1835
7 com.apple.applescript 0x00007fff56efa81a UASRemoteSend(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char*) + 498
8 com.apple.applescript 0x00007fff56ef1368 UASRemoteGetData(TUASObjectAlias*, TUASApplication*, TUASClassIdentifier*, unsigned char*) + 934
9 com.apple.applescript 0x00007fff56f04c43 UASGetDataNoCircularities(unsigned char, TUASClassIdentifier*) + 139
10 com.apple.applescript 0x00007fff56f04cbc UASGetData(unsigned char, TUASClassIdentifier*) + 54
11 com.apple.applescript 0x00007fff56ef0edf TUASObjectAlias::DidBecomeType(TUASClassIndex, TUASValue*&, TUASClassIdentifier*) + 95
12 com.apple.applescript 0x00007fff56f0306e TUASValue::BecomeType(TUASValue*&, TUASClassIdentifier*) + 64
13 com.apple.applescript 0x00007fff56efe6fa BCNot() + 38
14 com.apple.applescript 0x00007fff56edd9bf UASExecute1() + 1037
15 com.apple.applescript 0x00007fff56edd56f UASExecute(unsigned char) + 192
16 com.apple.applescript 0x00007fff56eac8b5 ASExecute(unsigned int, unsigned int, int, unsigned int*) + 464
17 com.apple.applescript 0x00007fff56ea8cb4 AppleScriptComponent + 1536
18 com.apple.applescript 0x00007fff56ec2e3b AGenericCall::Delegate(ComponentInstanceRecord*) + 37
19 com.apple.applescript 0x00007fff56ec2e01 AGenericManager::HandleOSACall(ComponentParameters*) + 57
20 com.apple.applescript 0x00007fff56ec23e1 GenericComponent + 161
21 com.apple.openscripting 0x00007fff45365eff OSAExecute + 54
22 com.apple.Foundation 0x00007fff48255e01 -[NSAppleScript(NSPrivate) _executeWithMode:andReturnError:] + 122
23 MBS_MacCocoa_NSAppleScript_Plugin_19677.dylib 0x000000010c842d99 executeAndReturnError(REALobjectStruct*, _REALdictionary*&) + 169
24 com.mothsoftware.mailarchiverx 0x0000000105505007 NSAppleScriptMBS.execute%o%o&o + 71
25 com.mothsoftware.mailarchiverx 0x0000000105835590 RunAS.executeScript%%o + 896
26 com.mothsoftware.mailarchiverx 0x0000000105837ee8 RunAS.ExecuteScriptWithParameters%%oo + 696
27 com.mothsoftware.mailarchiverx 0x00000001064c8592 emlxIterator.getHeader%s%o + 3122[/quote]

I have never seen such a crash myself. I only know that this usually happens for Mail and large mailboxes:

The crashing script is this one:

[code]Private Function MakeScriptTextHeader() as String()

dim theScript(-1) as String
for currentDepth as Integer = 0 to CountFields(MailboxPath, globals.MailboxDivider) - 1
theScript.append “property Mailbox” + Str(currentDepth) + “: ‘’”
next
theScript.Append “property currentMessageStr: ‘0’”
theScript.Append “property NoOfMailsStr : ‘1’”
theScript.Append “set currentMessage to currentMessageStr as integer”
theScript.append “set AllHeaders to {}”
theScript.append “tell application ‘Mail’”
theScript.Append “try”
theScript.Append “repeat with messageNo from currentMessage to currentMessage + 9”
if doAlternate then
theScript.append "set theMessage to (message MessageNo of " + AlternateMailboxString + “)”
else
theScript.append "set theMessage to (message MessageNo of " + MailboxString + “)”
end if

theScript.append “if not deleted status of theMessage then”
theScript.append “set theRes to (all headers of theMessage)”
theScript.append “if theRes = ‘’ then”
theScript.append “copy (‘empty’) to end of AllHeaders”
theScript.append “else”
theScript.append “copy theRes to end of AllHeaders”
theScript.append “end if”
theScript.append “else”
theScript.append “copy (‘deleted’) to end of AllHeaders”
theScript.append “end if”

theScript.append “end repeat”
theScript.append “return AllHeaders”
theScript.append “on error errMsg number errNr”
theScript.append “if errNr = -1719 then”
theScript.append “return AllHeaders”
theScript.append “end if”
theScript.append “end try”
theScript.append “end tell”

Return theScript

End Function[/code]

The script is executed lots of times so that I use NSAutoreleasePoolMBS regularly.

Any ideas how to solve this?

Xojo 2018r1, MBS from June 2018, High Sierra.

Maybe try without NSAutoreleasePoolMBS?

This is a crash in Apple’s frameworks…

I thought I needed to to the AutoReleasePool for managing memory? But I’ll have a look.

The issue usually shows up for large mailboxes (>10k). I could recompile the script every couple of thousand mails.

Problem with autorelease pools is that if the framework developer didn’t retain objects, things will crash.

As expected without the AutoReleasePool the memory goes only up and up. Even with 64bit this is not a good idea.

Anything else I can try except the recompile?

Perhaps this makes problems: there are 2 methods for getting data out of Mail - for the header and the body. Both have their own count for doing the AutoReleasePool. I could change this to one unified count.

maybe you make a dialog for import and a timer there, which runs the method per mailbox in the timer’s action?
This way main thread runs and you get autorelease pools handled by the system.

After implementing your idea I get nice crashes:

[quote]Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libobjc.A.dylib 0x00007fff88818af1 (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 403
1 com.apple.CoreFoundation 0x00007fff93d48bc2 _CFAutoreleasePoolPop + 50
2 com.apple.Foundation 0x00007fff8a82c8d5 -[NSAutoreleasePool release] + 146
3 MBS_Main_NSBase_Plugin_19677.dylib 0x0000000116994f31 NSAutoreleasePoolDestructor(REALobjectStruct*) + 81
4 Mail Archiver X.debug 0x000000010c5abc06 NSAutoreleasePoolMBS.__exit%%o + 54
5 XojoFramework 0x000000010f7535fb RuntimeUnlockObject + 815
6 Mail Archiver X.debug 0x000000010d03948c ProgressWindow.ProgressWindow.Timer1_Action%%o<ProgressWindow.ProgressWindow>o + 284 (/ProgressWindow:166)
7 XojoFramework 0x000000010f759039 0x10f4fd000 + 2474041
8 com.apple.CoreFoundation 0x00007fff93d92ae4 CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION + 20
9 com.apple.CoreFoundation 0x00007fff93d92773 __CFRunLoopDoTimer + 1075
10 com.apple.CoreFoundation 0x00007fff93d922ca __CFRunLoopDoTimers + 298
11 com.apple.CoreFoundation 0x00007fff93d897c1 __CFRunLoopRun + 1841
12 com.apple.CoreFoundation 0x00007fff93d88e28 CFRunLoopRunSpecific + 296
13 com.apple.HIToolbox 0x00007fff85da0935 RunCurrentEventLoopInMode + 235
14 com.apple.HIToolbox 0x00007fff85da076f ReceiveNextEventCommon + 432
15 com.apple.HIToolbox 0x00007fff85da05af _BlockUntilNextEventMatchingListInModeWithFilter + 71
16 com.apple.AppKit 0x00007fff8f5cbdf6 _DPSNextEvent + 1067
17 com.apple.AppKit 0x00007fff8f5cb226 -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454
18 XojoFramework 0x000000010f5d06a9 0x10f4fd000 + 865961
19 XojoFramework 0x000000010f5d06ed 0x10f4fd000 + 866029
20 Mail Archiver X.debug 0x000000010bc44bd5 Application._CallFunctionWithExceptionHandling%%op + 181
21 XojoFramework 0x000000010f750e53 CallFunctionWithExceptionHandling(void (*)()) + 262
22 XojoFramework 0x000000010f5d0656 0x10f4fd000 + 865878
23 com.apple.AppKit 0x00007fff8f5bfd80 -[NSApplication run] + 682
24 XojoFramework 0x000000010f74f17f RuntimeRun + 40[/quote]

Can I trap this somehow?