0

I have weird, though rare, crashes in my application.

I suspect that it happens because runModal of NSSavePanel is called in a thread different from the main thread.

Am I correct that it might be the cause for the crashes?

Monolo
  • 18,205
  • 17
  • 69
  • 103
Erik Sapir
  • 23,209
  • 28
  • 81
  • 141

3 Answers3

1

See the Threading Programming Guide from Apple:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html

"Thread-Unsafe Classes: NSWindow and all of its descendants."

NSSavePanel is a descendant of NSWindow.

Antoine Rosset
  • 1,045
  • 2
  • 13
  • 22
1

Yes.

I've had crashes calling runModal on the wrong thread. performSelectorOnMainThread is often useful to fix this.

Peter Tseng
  • 13,613
  • 4
  • 67
  • 57
  • Does `performSelectorOnMainThread` work on desktop osx? I cant find it anywhere in the docs, do you know where it is? Thx! – Noitidart Jul 25 '15 at 23:28
  • 1
    Haven't worked on OSX in a while, but maybe this?: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/#//apple_ref/occ/instm/NSObject/performSelectorOnMainThread:withObject:waitUntilDone: – Peter Tseng Jul 27 '15 at 17:57
0

As a general rule, all access to UI elements should happen from the main thread, also sometimes referred to as the UI thread.

So I'd say yes, you should open it from the main thread.

As to whether that is the source of your crashes is hard to tell. My modest experience tells me that crashes can be caused by all sorts of things...

Monolo
  • 18,205
  • 17
  • 69
  • 103