我正在使用Xcode 5.1.1编写Mac OS(10.8.4)应用程序,仅供我自己使用(当我遇到困难时解决八折单人纸牌游戏),我不在乎关于效率或内存使用情况。它是一个递归程序(它创建了一个可能移动的树,递归,创建下一步等等)所以它需要大量的内存(我已经有24 GB)并且我已经增加了堆栈(-Wl,-stack_size,0x4000000)。但是,无论堆栈大小如何,我都得到EXC_BAD_ACCESS代码= 2,总是在相同的深度(大约170个级别),并且它总是在使用NSString stringWithFormat的行上:例如:
NSString *temp = [NSString stringWithFormat:@"%d",value]; // value is an integer
该行已在所有先前的递归级别中执行得很好。如果我删除该特定行,则异常发生在某些其他stringWithFormat行。无论我的堆栈大小设置如何,NSString都有自己的限制。当使用垃圾收集而不是ARC时,它工作得更早。 (Xcode希望我转换为ARC。)
我已经检查过Zombies,但无论如何都没有在错误行上引用任何对象。
为什么我可以获得EXC_BAD_ACCESS而不管堆栈大小,并且总是在stringWithFormat上:?
添加崩溃日志:
Process: EightOff Solver [11701]
Path: /Users/USER/Library/Developer/Xcode/DerivedData/EightOff_Solver-dqppnsumvcsujjflailrgqxogyij/Build/Products/Debug/EightOff Solver.app/Contents/MacOS/EightOff Solver
Identifier: com.yourcompany.EightOff_Solver
Version: 1.0
Code Type: X86-64 (Native)
Parent Process: launchd [179]
User ID: 501
Date/Time: 2014-06-25 01:38:46.581 -0500
OS Version: Mac OS X 10.8.4 (12E55)
Report Version: 10
Sleep/Wake UUID: 561695D4-0498-43DC-9B0E-9417A5D5120D
Interval Since Last Report: 60266 sec
Crashes Since Last Report: 2
Per-App Crashes Since Last Report: 9
Anonymous UUID: 96F16B3C-9A73-63C2-B5CD-11CC51CFE612
Crashed Thread: 1 Dispatch queue: com.apple.root.default-priority
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000100400ec8
VM Regions Near 0x100400ec8:
MALLOC_TINY 0000000100100000-0000000100400000 [ 3072K] rw-/rwx SM=PRV
--> STACK GUARD 0000000100400000-0000000100401000 [ 4K] ---/rwx SM=NUL stack guard for thread 1
Stack 0000000100401000-0000000100483000 [ 520K] rw-/rwx SM=COW thread 1
Thread 0:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x00007fff8eb18686 mach_msg_trap + 10
1 libsystem_kernel.dylib 0x00007fff8eb17c42 mach_msg + 70
2 com.apple.CoreFoundation 0x00007fff93d38233 __CFRunLoopServiceMachPort + 195
3 com.apple.CoreFoundation 0x00007fff93d3d916 __CFRunLoopRun + 1078
4 com.apple.CoreFoundation 0x00007fff93d3d0e2 CFRunLoopRunSpecific + 290
5 com.apple.HIToolbox 0x00007fff93a36eb4 RunCurrentEventLoopInMode + 209
6 com.apple.HIToolbox 0x00007fff93a36c52 ReceiveNextEventCommon + 356
7 com.apple.HIToolbox 0x00007fff93a36ae3 BlockUntilNextEventMatchingListInMode + 62
8 com.apple.AppKit 0x00007fff9a52a533 _DPSNextEvent + 685
9 com.apple.AppKit 0x00007fff9a529df2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
10 com.apple.AppKit 0x00007fff9a5211a3 -[NSApplication run] + 517
11 com.apple.AppKit 0x00007fff9a4c5bd6 NSApplicationMain + 869
12 com.yourcompany.EightOff_Solver 0x0000000100006ea2 main + 34 (main.m:13)
13 com.yourcompany.EightOff_Solver 0x0000000100001bc4 start + 52
Thread 1 Crashed:: Dispatch queue: com.apple.root.default-priority
0 libsystem_c.dylib 0x00007fff936c682d __vfprintf + 40
1 libsystem_c.dylib 0x00007fff936c4e16 vsnprintf_l + 254
2 libsystem_c.dylib 0x00007fff936bf462 snprintf_l + 127
3 com.apple.CoreFoundation 0x00007fff93d0ef3f __CFStringAppendFormatCore + 11199
4 com.apple.CoreFoundation 0x00007fff93d594fb _CFStringCreateWithFormatAndArgumentsAux + 107
5 com.apple.Foundation 0x00007fff9a03f13c +[NSString stringWithFormat:] + 170
6 com.yourcompany.EightOff_Solver 0x0000000100009b67 -[Card description] + 663 (Card.m:265)
7 com.yourcompany.EightOff_Solver 0x00000001000091f7 -[Card makeNewGameStates:] + 9031 (Card.m:211)
8 com.yourcompany.EightOff_Solver 0x0000000100005087 -[MyDocument recursiveOnGameState:depth:] + 6903 (MyDocument.m:1195)
9 com.yourcompany.EightOff_Solver 0x00000001000054b2 -[MyDocument recursiveOnGameState:depth:] + 7970 (MyDocument.m:1210)
10 com.yourcompany.EightOff_Solver 0x00000001000054b2 -[MyDocument recursiveOnGameState:depth:] + 7970 (MyDocument.m:1210)
... [deleted identical lines numbered 11 through 170]
171 com.yourcompany.EightOff_Solver 0x00000001000054b2 -[MyDocument recursiveOnGameState:depth:] + 7970 (MyDocument.m:1210)
172 com.yourcompany.EightOff_Solver 0x00000001000054b2 -[MyDocument recursiveOnGameState:depth:] + 7970 (MyDocument.m:1210)
173 com.yourcompany.EightOff_Solver 0x00000001000054b2 -[MyDocument recursiveOnGameState:depth:] + 7970 (MyDocument.m:1210)
174 com.yourcompany.EightOff_Solver 0x00000001000054b2 -[MyDocument recursiveOnGameState:depth:] + 7970 (MyDocument.m:1210)
175 com.yourcompany.EightOff_Solver 0x00000001000054b2 -[MyDocument recursiveOnGameState:depth:] + 7970 (MyDocument.m:1210)
176 com.yourcompany.EightOff_Solver 0x00000001000054b2 -[MyDocument recursiveOnGameState:depth:] + 7970 (MyDocument.m:1210)
177 com.yourcompany.EightOff_Solver 0x00000001000029c2 -[MyDocument startBackgroundThread] + 2706 (MyDocument.m:882)
178 com.apple.CoreFoundation 0x00007fff93d8f09c __invoking___ + 140
179 com.apple.CoreFoundation 0x00007fff93d8ef37 -[NSInvocation invoke] + 263
180 com.apple.Foundation 0x00007fff9a07da30 -[NSInvocationOperation main] + 34
181 com.apple.Foundation 0x00007fff9a075926 -[__NSOperationInternal start] + 684
182 com.apple.Foundation 0x00007fff9a07d0f1 __block_global_6 + 129
183 libdispatch.dylib 0x00007fff96ec4f01 _dispatch_call_block_and_release + 15
184 libdispatch.dylib 0x00007fff96ec10b6 _dispatch_client_callout + 8
185 libdispatch.dylib 0x00007fff96ec21fa _dispatch_worker_thread2 + 304
186 libsystem_c.dylib 0x00007fff93654d0b _pthread_wqthread + 404
187 libsystem_c.dylib 0x00007fff9363f1d1 start_wqthread + 13
Thread 2:: Dispatch queue: com.apple.libdispatch-manager
0 libsystem_kernel.dylib 0x00007fff8eb1ad16 kevent + 10
1 libdispatch.dylib 0x00007fff96ec3dea _dispatch_mgr_invoke + 883
2 libdispatch.dylib 0x00007fff96ec39ee _dispatch_mgr_thread + 54
Thread 3:
0 libsystem_kernel.dylib 0x00007fff8eb1a6d6 __workq_kernreturn + 10
1 libsystem_c.dylib 0x00007fff93654f4c _pthread_workq_return + 25
2 libsystem_c.dylib 0x00007fff93654d13 _pthread_wqthread + 412
3 libsystem_c.dylib 0x00007fff9363f1d1 start_wqthread + 13
Thread 4:
0 libsystem_kernel.dylib 0x00007fff8eb1a6d6 __workq_kernreturn + 10
1 libsystem_c.dylib 0x00007fff93654f4c _pthread_workq_return + 25
2 libsystem_c.dylib 0x00007fff93654d13 _pthread_wqthread + 412
3 libsystem_c.dylib 0x00007fff9363f1d1 start_wqthread + 13
Thread 5:
0 libsystem_kernel.dylib 0x00007fff8eb1a6d6 __workq_kernreturn + 10
1 libsystem_c.dylib 0x00007fff93654f4c _pthread_workq_return + 25
2 libsystem_c.dylib 0x00007fff93654d13 _pthread_wqthread + 412
3 libsystem_c.dylib 0x00007fff9363f1d1 start_wqthread + 13
Thread 6:
0 libsystem_kernel.dylib 0x00007fff8eb1a6d6 __workq_kernreturn + 10
1 libsystem_c.dylib 0x00007fff93654f4c _pthread_workq_return + 25
2 libsystem_c.dylib 0x00007fff93654d13 _pthread_wqthread + 412
3 libsystem_c.dylib 0x00007fff9363f1d1 start_wqthread + 13
Thread 1 crashed with X86 Thread State (64-bit):
rax: 0x0000000000000208 rbx: 0x00007fff7ecb5a98 rcx: 0x00000001004014c0 rdx: 0x0000000100402d30
rdi: 0x0000000100401338 rsi: 0x00007fff7ecb5a98 rbp: 0x0000000100401250 rsp: 0x0000000100400e20
r8: 0x00000001004014c0 r9: 0x0000000000000003 r10: 0x0000000000000000 r11: 0x00000001004012b4
r12: 0x00000000000001ff r13: 0x0000000100401500 r14: 0x00000001004014c0 r15: 0x0000000100402d30
rip: 0x00007fff936c682d rfl: 0x0000000000010202 cr2: 0x0000000100400ec8
Logical CPU: 0
6/30/14编辑添加:这只是在我将Xcode升级到5.1.1(从5.0开始)时才开始发生的。可能是由于更改了构建设置? (我以前不知道它们是什么。)我使用的是iMac(2012年末)Intel Core i5,OS 10.8.4。一些设置是:基础SDK:最新(我已尝试限制为10.8);有效架构i386,x86_64(我尝试过只有i386或者只是x86_64)。部署目标:10.8。
7/1/14编辑添加:我正在使用NSOperationQueue(在主要队列上使用dispatch_async进行UI操作)。删除NSOperationQueue修复了崩溃,但现在我无法让我的UI更新(NSLog给了我一些输出)。在具有后台队列的dispatch_async()中执行我的循环也会崩溃,并且再次没有UI更新。显然,后台任务有一个与NSString相关的资源限制,我无法更改。即使我没有更新我的UI,它也会崩溃(总是创建一个NSString)。
答案 0 :(得分:0)
通过删除dispatch_async()并在主线程上运行它来解决问题的事实让我相信你确实在线程中耗尽了堆栈内存。我刚遇到了类似的问题(EXC_BAD_ACCESS位于深度递归函数集中的各种函数的顶部),并且能够通过增加堆栈大小来修复它。我似乎无法使用那些-Wl,-stack_size,...标志增加大小,也不使用NSThreads。 (也许这就是为什么它没有帮助你。)而是我使用pthread_create()启动线程,首先在其上调用pthread_attr_setstacksize()。代码示例:https://developer.apple.com/library/content/qa/qa1419/_index.html