initton 메소드에 싱글 톤 클래스가 있는데, NSMutableArray
을 설치하고 나중에 저장할 때이 크래시 로그가 무작위 적으로 크래시 될 수 있습니다. recordUploadCnt
을 사용할 수 없습니다. , 그러나 어디서 잘못되었는지 알 수는 없습니다.NSUserDefaults setObject 크래시
NSString* const KEY_SENSOR_UPLOAD = @"recordUploadCnt";
@property(nonatomic, strong) NSMutableArray* recordUploadCnt;
- (id)init {
if (self = [super init]) {
NSMutableArray* cnt = [[NSUserDefaults standardUserDefaults] objectForKey:KEY_SENSOR_UPLOAD];
self.recordUploadCnt = cnt ? [NSMutableArray arrayWithArray:cnt] : [NSMutableArray new];
}
return self;
}
double currentTime = [[NSDate date] timeIntervalSince1970];
if ([self.recordUploadCnt count] < 6) {
[self.recordUploadCnt addObject:[NSString stringWithFormat:@"%f",currentTime]];
} else {
[self.recordUploadCnt removeObjectAtIndex:0];
[self.recordUploadCnt addObject:[NSString stringWithFormat:@"%f",currentTime]];
}
[[NSUserDefaults standardUserDefaults] setObject:self.recordUploadCnt forKey:KEY_SENSOR_UPLOAD];
[[NSUserDefaults standardUserDefaults] synchronize];
Exception Type: SIGSEGV
Exception Codes: SEGV_MAPERR at 0xebfecbeb8
Crashed Thread: 37
Thread 37 Crashed:
0 libobjc.A.dylib 0x0000000184980430 _objc_msgSend :16 (in libobjc.A.dylib)
1 CoreFoundation 0x00000001856bf4c8 __CFPropertyListIsValidAux :52 (in CoreFoundation)
2 CoreFoundation 0x00000001856c02a4 __CFPropertyListIsArrayPlistAux :40 (in CoreFoundation)
3 CoreFoundation 0x00000001855e3900 _CFArrayApplyFunction :80 (in CoreFoundation)
4 CoreFoundation 0x00000001856bf5fc __CFPropertyListIsValidAux :360 (in CoreFoundation)
5 CoreFoundation 0x00000001855fc414 _CFPropertyListWrite :96 (in CoreFoundation)
6 CoreFoundation 0x00000001855fbc14 _CFPropertyListCreateData :316 (in CoreFoundation)
7 CoreFoundation 0x0000000185778f28 _CFPrefsEncodeKeyValuePairIntoMessage :504 (in CoreFoundation)
8 CoreFoundation 0x0000000185689f94 -[CFPrefsPlistSource sendMessageSettingValue:forKey:] :136 (in CoreFoundation)
9 CoreFoundation 0x00000001856886ec -[CFPrefsPlistSource alreadylocked_setValues:forKeys:count:from:] :440 (in CoreFoundation)
10 CoreFoundation 0x0000000185756034 -[CFPrefsSource setValues:forKeys:count:removeValuesForKeys:count:from:] :220 (in CoreFoundation)
11 CoreFoundation 0x00000001856b9150 -[CFPrefsSearchListSource alreadylocked_setValues:forKeys:count:from:] :584 (in CoreFoundation)
12 CoreFoundation 0x0000000185756034 -[CFPrefsSource setValues:forKeys:count:removeValuesForKeys:count:from:] :220 (in CoreFoundation)
13 CoreFoundation 0x00000001857563f4 -[CFPrefsSource setValue:forKey:from:] :64 (in CoreFoundation)
14 CoreFoundation 0x00000001856bbfa4 __108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke :260 (in CoreFoundation)
15 CoreFoundation 0x00000001856bb7c8 _normalizeQuintuplet :356 (in CoreFoundation)
16 CoreFoundation 0x00000001856bbe94 -[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:] :108 (in CoreFoundation)
17 CoreFoundation 0x000000018576008c -[_CFXPreferences setValue:forKey:appIdentifier:container:configurationURL:] :92 (in CoreFoundation)
18 CoreFoundation 0x000000018576370c _CFPreferencesSetAppValueWithContainer :128 (in CoreFoundation)
19 Foundation 0x000000018601bf80 -[NSUserDefaults(NSUserDefaults) setObject:forKey:] :68 (in Foundation)
동시성에 문제가있는 것처럼 보입니다. '@sync' 또는 직렬 디스패치 큐를 사용하여 배열이나 사용자 기본 키에 대한 동시 업데이트가 없도록해야합니다. 또한, 이것은 적절한 싱글 톤 패턴이 아닙니다. 'init'에서'dispatch_once'를 사용해야합니다. – Paulw11
@ Paulw11, 그건 의미가있어, 나는'dispatch_once'를 사용하여 게시물의'init' 메소드를 호출하는'sharedInstance' 메소드를 사용하고 있습니다. 이 충돌을 재현 할 수 있습니까? 나는 그것을 시도하지 않았다. – gabbler
@ gabbber, 기본 객체는 NSData, NSString, NSNumber, NSDate, NSArray 또는 NSDictionary의 인스턴스 (또는 컬렉션의 인스턴스 조합) 인 속성 목록이어야합니다. 다른 유형의 객체를 저장하려면 일반적으로 객체 유형을 저장하여 NSData의 인스턴스를 만들어야합니다. StackOverflow 링크를 시도해보십시오. https://stackoverflow.com/questions/6696558/storing-data-to-nsuserdefaults – Ashish