除非从Xcode运行,否则应用程序崩溃

时间:2012-01-21 13:30:26

标签: objective-c xcode macos applescript

我有这段代码:

NSColor *color = [NSColor colorWithDeviceHue:hue
                                  saturation:saturation
                                  brightness:brightness
                                       alpha:1.0];

NSString *scriptString = [NSString stringWithFormat:
                            @"try\n"
                                @"tell application \"Adobe Photoshop CS5\" \n"
                                    @"set foreground color to {class:RGB color, red:%f, green:%f, blue:%f} \n"
                                @"end tell\n"
                            @"on error\n"
                                @"try\n"
                                    @"tell application \"Adobe Photoshop CS4\" \n"
                                        @"set foreground color to {class:RGB color, red:%f, green:%f, blue:%f} \n"
                                    @"end tell\n"
                                @"end try\n"
                            @"end try",
                            [color redComponent] * 255,
                            [color greenComponent] * 255,
                            [color blueComponent] * 255];
NSLog(@"script string: %@", scriptString);

NSAppleScript *script = [[NSAppleScript alloc] initWithSource:scriptString];
NSDictionary *error;
[script executeAndReturnError:&error];
NSLog(@"result: %@", error);

当我从xcode的运行功能运行它时,它运行正常。当我将它构建为一个应用程序并独立运行时,它经常在这部分崩溃。

知道为什么会这样吗?

编辑:大部分崩溃日志(在这里都不适合)

Process:         The Color Picker Sync [1283]
Path:            /Users/USER/*/The Color Picker Sync.app/Contents/MacOS/The Color Picker Sync
Identifier:      com.Loupe.The-Color-Picker-Sync
Version:         1.1 (1.1)
Code Type:       X86-64 (Native)
Parent Process:  launchd [160]

Date/Time:       2012-01-21 14:04:54.025 +0000
OS Version:      Mac OS X 10.7.2 (11C74)
Report Version:  9
Sleep/Wake UUID: 6BFBE167-51ED-433B-BB70-17D6C4054817

Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: 0x000000000000000d, 0x0000000000000000

VM Regions Near 0:
--> 
    __TEXT                 0000000101782000-000000010178c000 [   40K] r-x/rwx SM=COW  /Users/USER/*/The Color Picker Sync.app/Contents/MacOS/The Color Picker Sync

Application Specific Information:
objc_msgSend() selector name: respondsToSelector:
objc[1283]: garbage collection is OFF

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib                 0x00007fff90d8114d objc_msgSend_vtable5 + 13
1   com.apple.Foundation            0x00007fff8c239456 _NSDescriptionWithLocaleFunc + 50
2   com.apple.CoreFoundation        0x00007fff911284d5 __CFStringAppendFormatCore + 11365
3   com.apple.CoreFoundation        0x00007fff9117acdb _CFStringCreateWithFormatAndArgumentsAux + 107
4   com.apple.CoreFoundation        0x00007fff911f3182 _CFLogvEx + 114
5   com.apple.Foundation            0x00007fff8c2b3b53 NSLogv + 89
6   com.apple.Foundation            0x00007fff8c2b3af1 NSLog + 130
7   com.Loupe.The-Color-Picker-Sync 0x0000000101784103 -[StatusMenuItemSampleAppDelegate didReceiveMessage:] + 1023 (StatusMenuItemSampleAppDelegate.m:294)
8   com.Loupe.The-Color-Picker-Sync 0x00000001017848fb -[LocalRoom receivedNetworkPacket:viaConnection:] + 104 (LocalRoom.m:161)
9   com.Loupe.The-Color-Picker-Sync 0x0000000101785aa2 -[NetworkConnection readFromStreamIntoIncomingBuffer] + 416 (NetworkConnection.m:359)
10  com.apple.CoreFoundation        0x00007fff91183b7b _signalEventSync + 107
11  com.apple.CoreFoundation        0x00007fff91183b02 _cfstream_solo_signalEventSync + 98
12  com.apple.CoreFoundation        0x00007fff91183928 _CFStreamSignalEvent + 632
13  com.apple.CFNetwork             0x00007fff87459613 SocketStream::dispatchSignalFromSocketCallbackUnlocked(SocketStreamSignalHolder*) + 45
14  com.apple.CFNetwork             0x00007fff87458c72 SocketStream::socketCallback(__CFSocket*, unsigned long, __CFData const*, void const*) + 232
15  com.apple.CFNetwork             0x00007fff87458b72 SocketStream::_SocketCallBack_stream(__CFSocket*, unsigned long, __CFData const*, void const*, void*) + 100
16  com.apple.CoreFoundation        0x00007fff9117b714 __CFSocketPerformV0 + 980
17  com.apple.CoreFoundation        0x00007fff91133b51 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18  com.apple.CoreFoundation        0x00007fff911333bd __CFRunLoopDoSources0 + 253
19  com.apple.CoreFoundation        0x00007fff9115a1a9 __CFRunLoopRun + 905
20  com.apple.CoreFoundation        0x00007fff91159ae6 CFRunLoopRunSpecific + 230
21  com.apple.HIToolbox             0x00007fff8e14d3d3 RunCurrentEventLoopInMode + 277
22  com.apple.HIToolbox             0x00007fff8e15463d ReceiveNextEventCommon + 355
23  com.apple.HIToolbox             0x00007fff8e1544ca BlockUntilNextEventMatchingListInMode + 62
24  com.apple.AppKit                0x00007fff8c5533f1 _DPSNextEvent + 659
25  com.apple.AppKit                0x00007fff8c552cf5 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135
26  com.apple.AppKit                0x00007fff8c54f62d -[NSApplication run] + 470
27  com.apple.AppKit                0x00007fff8c7ce80c NSApplicationMain + 867
28  com.Loupe.The-Color-Picker-Sync 0x000000010178302c 0x101782000 + 4140

Thread 1:: Dispatch queue: com.apple.libdispatch-manager
0   libsystem_kernel.dylib          0x00007fff8b3cc7e6 kevent + 10
1   libdispatch.dylib               0x00007fff8bf615be _dispatch_mgr_invoke + 923
2   libdispatch.dylib               0x00007fff8bf6014e _dispatch_mgr_thread + 54

Thread 2:: com.apple.CFSocket.private
0   libsystem_kernel.dylib          0x00007fff8b3cbdf2 __select + 10
1   com.apple.CoreFoundation        0x00007fff911a2f9b __CFSocketManager + 1355
2   libsystem_c.dylib               0x00007fff90aad8bf _pthread_start + 335
3   libsystem_c.dylib               0x00007fff90ab0b75 thread_start + 13

Thread 3:
0   libsystem_kernel.dylib          0x00007fff8b3cc192 __workq_kernreturn + 10
1   libsystem_c.dylib               0x00007fff90aaf594 _pthread_wqthread + 758
2   libsystem_c.dylib               0x00007fff90ab0b85 start_wqthread + 13

Thread 4:
0   libsystem_kernel.dylib          0x00007fff8b3cc192 __workq_kernreturn + 10
1   libsystem_c.dylib               0x00007fff90aaf594 _pthread_wqthread + 758
2   libsystem_c.dylib               0x00007fff90ab0b85 start_wqthread + 13

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x0000000000000000  rcx: 0x00007fff6137ec80  rdx: 0x00007fff8c481ccc
  rdi: 0x20200a3b31203d20  rsi: 0x00007fff76a0f730  rbp: 0x00007fff6137e970  rsp: 0x00007fff6137e958
   r8: 0x000000000000000a   r9: 0x000000000000057e  r10: 0x0000000000000000  r11: 0x00007fca58557e21
  r12: 0x00007fff91128644  r13: 0x000000000000000a  r14: 0x20200a3b31203d20  r15: 0x0000000000000000
  rip: 0x00007fff90d8114d  rfl: 0x0000000000010246  cr2: 0x000000006a86fdc0
Logical CPU: 1



External Modification Summary:
  Calls made by other processes targeting this process:
    task_for_pid: 11
    thread_create: 0
    thread_set_state: 0
  Calls made by this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by all processes on this machine:
    task_for_pid: 4860
    thread_create: 1
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=211.3M resident=105.4M(50%) swapped_out_or_unallocated=105.9M(50%)
Writable regions: Total=48.2M written=3504K(7%) resident=19.3M(40%) swapped_out=1856K(4%) unallocated=29.0M(60%)

REGION TYPE                      VIRTUAL
===========                      =======
CG backing stores                     8K
CG raster data                       64K
CG shared images                    168K
CoreGraphics                         16K
CoreServices                       2004K
MALLOC                             22.0M
MALLOC guard page                    32K
Memory tag=240                        4K
Memory tag=242                       12K
Memory tag=243                       16K
Memory tag=249                      156K
STACK GUARD                        56.0M
Stack                              9820K
VM_ALLOCATE                        16.1M
__CI_BITMAP                          80K
__DATA                             21.8M
__IMAGE                            1256K
__LINKEDIT                         47.6M
__RC_CAMERAS                        236K
__TEXT                            163.7M
__UNICODE                           544K
mapped file                        17.1M
shared memory                       312K
===========                      =======
TOTAL                             358.7M

2 个答案:

答案 0 :(得分:2)

最常见的错误:NSAppleScript can only be used in the main app thread,因为它共享AppleScript ComponentInstance。但是如果你出现崩溃日志就会有所帮助。

答案 1 :(得分:0)

您最初需要将error设置为nil:

NSDictionary *error = nil;

[script executeAndReturnError:&error]仅在失败时才会更改error的值。这就是为什么如果没有错误你会得到一个糟糕的访问。