我想使用 NSFilemanager 中的打开文件对话框,但我的代码有时会崩溃,有时会起作用,我不知道为什么。有时它可以 100% 工作,有时窗口是空的,有时对话框后面的背景会显示在窗口中。当发生崩溃时,Xcode 中会显示“signal:SIGABRT”。
func openfiledlg (title: String, message: String) -> String
{
var myFiledialog: NSOpenPanel = NSOpenPanel()
myFiledialog.prompt = "Öffnen"
myFiledialog.worksWhenModal = true
myFiledialog.allowsMultipleSelection = false
myFiledialog.canChooseDirectories = false
myFiledialog.resolvesAliases = true
myFiledialog.title = title
myFiledialog.message = message
myFiledialog.runModal()
var chosenfile = myFiledialog.URL
if (chosenfile != nil)
{
var TheFile = chosenfile.absoluteString!
return (TheFile)
}
else
{
return ("")
}
}
我做错了什么?为什么会崩溃?
该应用程序不在主线程上运行。我总是打开一个新线程,它运行我的程序。主线程仅处理来自 SpriteKit 的屏幕更新,我将其用于我的程序。
我刚刚构建了一个新的基于 Cocoa 的应用程序,并让该函数在主线程中运行,它在那里工作。当我在 Cocoa-App 中启动一个线程时,它就像在 SpriteKit 环境中一样崩溃。
我需要在 Sprite-Kit 环境中启动一个新线程,因为如果我直接从 AppDelegate 启动我的主程序,更新将不会完成。主程序运行直到整个 SpriteKit 退出,所以我没有机会在主线程中完成我的工作。
崩溃发生在 runModal() 的行中,然后在“NSSavePanel._spAuxiliaryStorage”中:
0x7fff84dfec20: movq -0x10c18197(%rip), %rsi ; "_refreshDelegateOptions"
0x7fff84dfec27: movq %rbx, %rdi
0x7fff84dfec2a: callq *%r15
0x7fff84dfec2d: movq -0x10c17ed4(%rip), %rsi ; "_loadPreviousModeAndLayout"
0x7fff84dfec34: movq %rbx, %rdi
0x7fff84dfec37: callq *%r15
0x7fff84dfec3a: movq -0x10b57079(%rip), %r12 ; NSSavePanel._spAuxiliaryStorage <--- Thread 7: signal SIGABRT
0x7fff84dfec41: movq (%rbx,%r12), %rax
0x7fff84dfec45: movq -0x10b5716c(%rip), %rcx ; NSSavePanelAuxiliary._clientSetADirectory
0x7fff84dfec4c: movb (%rax,%rcx), %al
0x7fff84dfec4f: shrb $0x2, %al
0x7fff84dfec52: andb $0x1, %al
0x7fff84dfec54: xorb $0x1, %al
0x7fff84dfec56: movzbl %al, %ecx
0x7fff84dfec59: movq -0x10c18310(%rip), %rsi ; "_configureForDirectory:forceDefault:"
0x7fff84dfec60: movq %rbx, %rdi
0x7fff84dfec63: xorl %edx, %edx
0x7fff84dfec65: callq *%r15
0x7fff84dfec68: movq -0x10c2d767(%rip), %rsi ; "drain"
0x7fff84dfec6f: movq %r14, %rdi
0x7fff84dfec72: callq *%r15
0x7fff84dfec75: movq (%rbx,%r12), %rsi
在终端窗口中显示:
CocoaTest(54483,0x106f78000) malloc: *** error for object 0x60800017efc0: Heap corruption detected, free list canary is damaged
知道如何在不在主线程中执行此问题的情况下解决此问题吗?