我遇到了崩溃,很可能与我无法发现的内存管理有关。
崩溃从未发生在我身上,我只知道它已经发生,因为我收到了崩溃报告 这也意味着我必须确认崩溃的唯一当前方法是运送应用程序并等待崩溃报告进入(坏消息)或不(快乐我!)。
崩溃报告摘录:
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x9
Crashed Thread: 0
Thread 0 Crashed:
0 CoreFoundation 0x375f29e8 0x375e4000 + 59880
1 MyApp 0x000bf22f -[UIViewController(AddressPicker) fullNameAndAddressFromPerson:identifier:] (UIViewController+AddressPicker.m:108)
2 MyApp 0x000bf3f9 -[UIViewController(AddressPicker) guessedUserAddress] (UIViewController+AddressPicker.m:161)
3 MyApp 0x0009cc99 -[RecipientsViewController loadUserAddressFromMyAppSender] (RecipientsViewController.m:190)
4 MyApp 0x0009c189 -[RecipientsViewController viewDidLoad] (RecipientsViewController.m:78)
5 UIKit 0x31a5e541 0x31a3c000 + 140609
原始代码:
- (NSString *) fullNameAndAddressFromPerson:(ABRecordRef) person identifier:(ABMultiValueIdentifier) identifier {
NSMutableString *address = [NSMutableString new];
// Get and add first and last name
CFStringRef cfFirstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *firstName = (NSString *)CFBridgingRelease(cfFirstName);
CFStringRef cfLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *lastName = (NSString *)CFBridgingRelease(cfLastName);
[address appendFormat:@"%@ %@\n", firstName ?: @"", lastName ?: @""];
// Get and add address
ABMultiValueRef addressMultiValue = ABRecordCopyValue(person, kABPersonAddressProperty);
CFTypeRef addressRef = ABMultiValueCopyValueAtIndex(addressMultiValue, ABMultiValueGetIndexForIdentifier(addressMultiValue, identifier)); // This is line 108
CFRelease(addressMultiValue);
NSDictionary *addressDictionary = (NSDictionary *) (CFBridgingRelease(addressRef));
if ([addressDictionary isKindOfClass:NSDictionary.class]) {
[address appendString:ABCreateStringWithAddressDictionary(addressDictionary, YES)];
}
return [address stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
更新了代码,包括Nirav建议的检查:
- (NSString *) fullNameAndAddressFromPerson:(ABRecordRef) person identifier:(ABMultiValueIdentifier) identifier {
NSMutableString *address = [NSMutableString new];
NSString *firstName, *lastName;
// Get and add first and last name
if (person) {
CFStringRef cfFirstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
if (cfFirstName) {
firstName = (NSString *)CFBridgingRelease(cfFirstName);
}
CFStringRef cfLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
if (cfLastName) {
lastName = (NSString *)CFBridgingRelease(cfLastName);
}
}
[address appendFormat:@"%@ %@\n", firstName ?: @"", lastName ?: @""];
// Get and add address
ABMultiValueRef addressMultiValue = ABRecordCopyValue(person, kABPersonAddressProperty);
CFTypeRef addressRef;
if (addressMultiValue && identifier) {
addressRef = ABMultiValueCopyValueAtIndex(addressMultiValue, ABMultiValueGetIndexForIdentifier(addressMultiValue, identifier));
CFRelease(addressMultiValue);
}
NSDictionary *addressDictionary;
if (addressRef) {
addressDictionary = (NSDictionary *) (CFBridgingRelease(addressRef));
if ([addressDictionary isKindOfClass:NSDictionary.class]) {
[address appendString:ABCreateStringWithAddressDictionary(addressDictionary, YES)];
}
}
return [address stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
我想我错过了显而易见的事情,所以不是我回答“呃,你是对的”,我会接受可能修复错误的答案和指出这些代码的方式变得更安全和更好。
答案 0 :(得分:1)
我遇到了类似的崩溃做类似的事情,这就是我所做的:
等等。要点是在桥接宏之前执行空检查。
这是为了处理没有填充某些字段的电话簿记录。