1

I have this code

print(String(u))
print(String(viewDocumentController))

let doc = viewDocumentController.documentForURL(u)

where viewDocumentController is an instance of NSDocumentController. The first time I run this code in my app, it runs fine. But later I try to close the documents in the controller, either with

    viewDocumentController.closeAllDocumentsWithDelegate(nil,                                                                                      didCloseAllSelector: nil, contextInfo: nil)

or with

    for doc in viewDocumentController.documents
    {
        doc.canCloseDocumentWithDelegate(self, shouldCloseSelector: nil, contextInfo: nil)
    }

Either way, when I run the code at the top again after this closing of documents, the print statements indicate that neither u nor viewDocumentController is nil. And documentForURL is explicitly supposed to return an optional, so it shouldn't mind if it doesn't find any documents matching that URL - it should just return nil.

And yet, when I run the documentForURL line at the top - which had run fine before I closed the documents - the program crashes with the error "Thread 1: EXC_BAD_ACCESS (code=1, address=(0x0)". Which sure sounds like a null pointer exception. Why would documentForURL give me such an error here?

EDIT: I also tried looking for zombies, as per these instructions, and did not see any flags or Zombie Messaged dialogs.

EDIT: And here's the stack trace:

(lldb) po [NSThread callStackSymbols]
<_NSCallStackArray 0x608000258de0>(
0   ???                                 0x00000001079d7a05 0x0 + 4422728197,
1   Brouillon                           0x0000000100012720 main + 0,
2   AppKit                              0x00007fff947f5d06 -[NSDocument _something:didSomething:soContinue:] + 17,
3   AppKit                              0x00007fff947f5cde __71-[NSDocument _saveIfNecessaryWithDelegate:didSaveSelector:contextInfo:]_block_invoke_3 + 386,
4   AppKit                              0x00007fff9479f6d1 -[NSDocument _discontinueFileAccessUsingBlock:] + 202,
5   AppKit                              0x00007fff9479f9f5 __67-[NSDocument autosaveWithImplicitCancellability:completionHandler:]_block_invoke + 320,
6   AppKit                              0x00007fff946bae2b -[NSDocument continueFileAccessUsingBlock:] + 222,
7   AppKit                              0x00007fff9497caeb __33-[NSDocument _performFileAccess:]_block_invoke769 + 125,
8   AppKit                              0x00007fff949b5f39 __67-[NSDocumentControllerMainThreadUnblockerEntry _invokeInterrupter:]_block_invoke + 19,
9   AppKit                              0x00007fff9497b7ca -[NSDocument _performFileAccess:] + 658,
10  AppKit                              0x00007fff9497cde9 -[NSDocument _synchronouslyCheckFileURLUsingBlock:] + 233,
11  AppKit                              0x00007fff947edc33 -[NSDocument _isLocatedByURL:becauseOfAutosavedContentsFile:] + 147,
12  AppKit                              0x00007fff94785a9b -[NSDocumentController _documentForURL:] + 207,
13  Brouillon                           0x0000000100005630 _TFC9Brouillon14ViewController21intakeFilesFromFolderfTCSo12NSMutableSet16currentFolderURLCSo5NSURL_T_ + 1360,
14  Brouillon                           0x000000010000501f _TFC9Brouillon14ViewController17createNewNotebookfCSo5NSURLT_ + 1151,
15  Brouillon                           0x0000000100002d45 _TFC9Brouillon14ViewController24checkForExistingNotebookfCSo5NSURLT_ + 3061,
16  Brouillon                           0x0000000100005ff5 _TFC9Brouillon14ViewController12openNotebookfCSo5NSURLT_ + 101,
17  Brouillon                           0x0000000100009149 _TTWC9Brouillon14ViewControllerS_18NotebookControllerS_FS1_12openNotebookfCSo5NSURLT_ + 73,
18  Brouillon                           0x000000010000c889 _TFFC9Brouillon16WindowController19openAnotherNotebookFPs9AnyObject_T_U_FSiT_ + 697,
19  Brouillon                           0x000000010000c9ef _TTRXFo_dSi_dT__XFdCb_dSi_dT__ + 47,
20  AppKit                              0x00007fff94ae78cc -[NSSavePanel _didEndSheet:returnCode:contextInfo:] + 95,
21  AppKit                              0x00007fff946ef0b0 -[NSWindow _endWindowBlockingModalSession:returnCode:] + 315,
22  AppKit                              0x00007fff94ae9fb6 -[NSSavePanel ok:] + 482,
23  libsystem_trace.dylib               0x00007fff9844407a _os_activity_initiate + 75,
24  AppKit                              0x00007fff946cce89 -[NSApplication sendAction:to:from:] + 460,
25  AppKit                              0x00007fff946defde -[NSControl sendAction:to:] + 86,
26  AppKit                              0x00007fff946def08 __26-[NSCell _sendActionFrom:]_block_invoke + 131,
27  libsystem_trace.dylib               0x00007fff9844407a _os_activity_initiate + 75,
28  AppKit                              0x00007fff946dee65 -[NSCell _sendActionFrom:] + 144,
29  libsystem_trace.dylib               0x00007fff9844407a _os_activity_initiate + 75,
30  AppKit                              0x00007fff946dd48a -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2693,
31  AppKit                              0x00007fff94725fd0 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 744,
32  AppKit                              0x00007fff946dbbb4 -[NSControl mouseDown:] + 669,
33  AppKit                              0x00007fff94c30469 -[NSWindow _handleMouseDownEvent:isDelayedEvent:] + 6322,
34  AppKit                              0x00007fff94c3144d -[NSWindow _reallySendEvent:isDelayedEvent:] + 212,
35  AppKit                              0x00007fff9467063d -[NSWindow sendEvent:] + 517,
36  AppKit                              0x00007fff945f0b3c -[NSApplication sendEvent:] + 2540,
37  AppKit                              0x00007fff94457ef6 -[NSApplication run] + 796,
38  AppKit                              0x00007fff9442146c NSApplicationMain + 1176,
39  Brouillon                           0x0000000100012774 main + 84,
40  libdyld.dylib                       0x00007fff9513a5ad start + 1,
41  ???                                 0x0000000000000003 0x0 + 3
)
Displaced Hoser
  • 871
  • 3
  • 13
  • 35
  • Can you post the full stack trace? Xcode is probably not showing you the code that caused the error, it's showing you the line of your code that called the code in the system that caused the error. – tbodt Jul 29 '16 at 04:27
  • Added above. Any ideas? – Displaced Hoser Jul 29 '16 at 10:25
  • Hmm... looks like some internal block context is getting trashed. No idea why... are you doing anything "off the beaten path" with the `NSDocumentController`? – tbodt Jul 29 '16 at 15:03
  • The big thing is I'm including multiple documents in a single window, so it's not a "document-based application" in Apple's sense; I use the document controller to handle saving to disk. So I access it directly (adding documents to it, closing them in it, etc.) rather than relying on the built-in "document architecture". – Displaced Hoser Jul 30 '16 at 02:20
  • Can `didCloseAllSelector` and `shouldCloseSelector` be nil? – Willeke Jul 30 '16 at 14:59
  • Well, [answers to my earlier question](http://stackoverflow.com/questions/38064830/proper-parameters-for-closealldocumentswithdelegate) had seemed to recommend I could just pass nil to the selector argument, and it compiled then. Do you think it might help to create an empty selector method just to stop this one from crashing? – Displaced Hoser Aug 01 '16 at 13:13

0 Answers0