NSUserDefaults integerForKey EXC_BAD_ACCESS?

NSUserDefaults integerForKey EXC_BAD_ACCESS?

我在我的应用程序中使用 NSUserDefaults。我收到了很多崩溃报告,其中包含崩溃的代码 [[NSUserDefaults standardUserDefaults] integerForKey:MY_KEY]; EXC_BAD_ACCESS.
常量在单独的 .h 头文件中定义。

static NSString *const MY_KEY = @"myKey";
static NSInteger const SOME_VALUE = 0;

部分控制器方法崩溃

- (void)displayData:(NSInteger)param {
      NSString *string1;
      NSString *string2;
      NSInteger value = [[NSUserDefaults standardUserDefaults] integerForKey: MY_KEY];
}

这是 Xcode Organizer 中的崩溃报告

这是崩溃日志

Incident Identifier: 1DD7D3CD-DE9B-4623-AB02-06D2365728DA
CrashReporter Key:   020e241bb4242d3e055b3eef0f3ab86f54af59df
Hardware Model:      iPhone6,2
Process:             My App [194]
Path:                /private/var/mobile/Containers/Bundle/Application/31286A32-F4D3-48E1-8B05-ECE119EA96A8/MyApp.app/My App
Identifier:          com.mycompany.myapp
Version:             1.0.0 (1.0.0)
Code Type:           ARM-64 (Native)
Parent Process:      launchd [1]

Date/Time:           2016-01-16 12:55:34.34 +0200
Launch Time:         2016-01-16 12:24:31.31 +0200
OS Version:          iOS 9.2 (13C75)
Report Version:      105

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x00000000158b6560
Triggered by Thread:  0

Filtered syslog:
None found

Global Trace Buffer (reverse chronological seconds):
1.629540     CFNetwork                  0x00000001851af104 TCP Conn 0x15a473640 complete. fd: 8, err: 0
1.629540     CFNetwork                  0x00000001851b0630 TCP Conn 0x15a473640 event 1. err: 0



Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libobjc.A.dylib                 0x00000001841b5bd0 objc_msgSend + 16
1   CoreFoundation                  0x0000000184b69118 __CFBasicHashAddValue + 328
2   CoreFoundation                  0x0000000184a179d8 CFBasicHashAddValue + 288
3   CoreFoundation                  0x0000000184a18c90 CFDictionaryAddValue + 248
4   CoreFoundation                  0x0000000184a305a8 __CFDictionaryApplyFunction_block_invoke + 24
5   CoreFoundation                  0x0000000184a189bc CFBasicHashApply + 128
6   CoreFoundation                  0x0000000184a21d3c CFDictionaryApplyFunction + 200
7   CoreFoundation                  0x0000000184abd8a8 -[CFPrefsPlistSource mergeIntoDictionary:] + 304
8   CoreFoundation                  0x0000000184ae45e0 -[CFPrefsSearchListSource alreadylocked_copyDictionary] + 828
9   CoreFoundation                  0x0000000184ae33fc -[CFPrefsSearchListSource alreadylocked_copyValueForKey:] + 72
10  CoreFoundation                  0x0000000184b74ee4 ___CFPreferencesCopyAppValueWithContainer_block_invoke + 68
11  CoreFoundation                  0x0000000184ae25b8 +[CFPrefsSearchListSource withSearchListForIdentifier:container:perform:] + 572
12  CoreFoundation                  0x0000000184b74e5c _CFPreferencesCopyAppValueWithContainer + 188
13  CoreFoundation                  0x0000000184b75130 _CFPreferencesGetAppIntegerValueWithContainer + 28
14  My App                          0x000000010006f520 -[MyController displayData:] (MyController.m:998)
15  Foundation                      0x0000000185425844 -[NSObject(NSThreadPerformAdditions) performSelector:onThread:withObject:waitUntilDone:modes:] + 988
16  Foundation                      0x00000001854296a0 -[NSObject(NSThreadPerformAdditions) performSelectorOnMainThread:withObject:waitUntilDone:] + 144
17  My App                          0x000000010006cd68 -[MyController StateDidChange:] (MyController.m:307)
18  Foundation                      0x00000001854f7e20 __NSThreadPerformPerform + 340
19  CoreFoundation                  0x0000000184aecefc __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
20  CoreFoundation                  0x0000000184aec990 __CFRunLoopDoSources0 + 540
21  CoreFoundation                  0x0000000184aea690 __CFRunLoopRun + 724
22  CoreFoundation                  0x0000000184a19680 CFRunLoopRunSpecific + 384
23  GraphicsServices                0x0000000185f28088 GSEventRunModal + 180
24  UIKit                           0x0000000189890d90 UIApplicationMain + 204
25  My App                          0x000000010006b6b4 main (main.m:16)
26  libdyld.dylib                   0x00000001845ba8b8 start + 4

Thread 1 name:  Dispatch queue: com.apple.libdispatch-manager
Thread 1:
0   libsystem_kernel.dylib          0x00000001846d94fc kevent_qos + 8
1   libdispatch.dylib               0x000000018459c94c _dispatch_mgr_invoke + 232
2   libdispatch.dylib               0x000000018458b7bc _dispatch_source_invoke + 0

Thread 2 name:  com.apple.NSURLConnectionLoader
Thread 2:
0   libsystem_kernel.dylib          0x00000001846bd4bc mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001846bd338 mach_msg + 72
2   CoreFoundation                  0x0000000184aecac0 __CFRunLoopServiceMachPort + 196
3   CoreFoundation                  0x0000000184aea7c4 __CFRunLoopRun + 1032
4   CoreFoundation                  0x0000000184a19680 CFRunLoopRunSpecific + 384
5   CFNetwork                       0x0000000185189434 +[NSURLConnection(Loader) _resourceLoadLoop:] + 412
6   Foundation                      0x00000001854f7c40 __NSThread__start__ + 1000
7   libsystem_pthread.dylib         0x000000018479fb28 _pthread_body + 156
8   libsystem_pthread.dylib         0x000000018479fa8c _pthread_body + 0
9   libsystem_pthread.dylib         0x000000018479d028 thread_start + 4

Thread 3 name:  com.apple.CFSocket.private
Thread 3:
0   libsystem_kernel.dylib          0x00000001846d8368 __select + 8
1   CoreFoundation                  0x0000000184af3028 __CFSocketManager + 648
2   libsystem_pthread.dylib         0x000000018479fb28 _pthread_body + 156
3   libsystem_pthread.dylib         0x000000018479fa8c _pthread_body + 0
4   libsystem_pthread.dylib         0x000000018479d028 thread_start + 4

Thread 4 name:  NetworkLoad
Thread 4:
0   libsystem_kernel.dylib          0x00000001846bd4bc mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001846bd338 mach_msg + 72
2   CoreFoundation                  0x0000000184aecac0 __CFRunLoopServiceMachPort + 196
3   CoreFoundation                  0x0000000184aea7c4 __CFRunLoopRun + 1032
4   CoreFoundation                  0x0000000184a19680 CFRunLoopRunSpecific + 384
5   GeoServices                     0x000000018b1e9b5c _runNetworkThread + 428
6   libsystem_pthread.dylib         0x000000018479fb28 _pthread_body + 156
7   libsystem_pthread.dylib         0x000000018479fa8c _pthread_body + 0
8   libsystem_pthread.dylib         0x000000018479d028 thread_start + 4

Thread 5:
0   libsystem_kernel.dylib          0x00000001846d8b6c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x000000018479d530 _pthread_wqthread + 1284
2   libsystem_pthread.dylib         0x000000018479d020 start_wqthread + 4

Thread 6:
0   libsystem_kernel.dylib          0x00000001846d8b6c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x000000018479d530 _pthread_wqthread + 1284
2   libsystem_pthread.dylib         0x000000018479d020 start_wqthread + 4

Thread 7:
0   libsystem_kernel.dylib          0x00000001846d8b6c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x000000018479d530 _pthread_wqthread + 1284
2   libsystem_pthread.dylib         0x000000018479d020 start_wqthread + 4

Thread 8:
0   libsystem_kernel.dylib          0x00000001846d8b6c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x000000018479d530 _pthread_wqthread + 1284
2   libsystem_pthread.dylib         0x000000018479d020 start_wqthread + 4

Thread 9:
0   libsystem_kernel.dylib          0x00000001846d7f48 __psynch_cvwait + 8
1   libsystem_pthread.dylib         0x000000018479ece8 _pthread_cond_wait + 648
2   Foundation                      0x0000000185462bac -[NSCondition wait] + 240
3   Foundation                      0x0000000185425780 -[NSObject(NSThreadPerformAdditions) performSelector:onThread:withObject:waitUntilDone:modes:] + 792
4   Foundation                      0x00000001854296a0 -[NSObject(NSThreadPerformAdditions) performSelectorOnMainThread:withObject:waitUntilDone:] + 144
5   My App                          0x0000000100085714 -[AppStateManager HandleAsynchRequest:] (AppStateManager.m:328)
6   Foundation                      0x00000001854f7c40 __NSThread__start__ + 1000
7   libsystem_pthread.dylib         0x000000018479fb28 _pthread_body + 156
8   libsystem_pthread.dylib         0x000000018479fa8c _pthread_body + 0
9   libsystem_pthread.dylib         0x000000018479d028 thread_start + 4

Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000000155616b10   x1: 0x000000018a206102   x2: 0x00000001556169c0   x3: 0x0000000155616b10
    x4: 0x000000015680c2c0   x5: 0x0000000000000010   x6: 0x0000000000000067   x7: 0x0000000000000fd0
    x8: 0x00000001a1223000   x9: 0x00000000158b6550  x10: 0x00000001a216a000  x11: 0x00000001a216a000
   x12: 0x00000001556169d0  x13: 0x10000000158b6554  x14: 0x000000000000008c  x15: 0x00000001556169dd
   x16: 0x5000f8d3b4fb11e2  x17: 0x00000001556169dd  x18: 0x0000000000000000  x19: 0x000000015680c240
   x20: 0x00000001556169c0  x21: 0x0000000155616b10  x22: 0x0000000000000001  x23: 0x00000001a2165360
   x24: 0x0000000184aee6c8  x25: 0x00000001a2165360  x26: 0x0000000000000003  x27: 0x0000000158ffa300
   x28: 0x00000000a7baadb1  fp: 0x000000016fd99f50   lr: 0x0000000184b69118
    sp: 0x000000016fd99f00   pc: 0x00000001841b5bd0 cpsr: 0x20000000

我在启用僵尸的情况下收到此消息

[CFString retain]: message sent to deallocated instance 0x12f78f7e0 

我在开发过程中复制了一次这个错误 iPhone。另请注意,该应用程序使用实时 TCP 通信。当我收到一些数据时,会创建一个块来显示 UI.

中的数据

NSUserDefaults 可以生成 EXC_BAD_ACCESS 吗?

这是一个与内存泄漏有关的问题。要解决此问题,请使用 Xcode 静态分析来查找代码中潜在的内存泄漏。还有 运行 Xcode 具有泄漏配置的仪器。