2

我发生了崩溃,很可能与我无法发现的内存管理有关。

崩溃从未发生在我身上,我只知道它发生是因为我收到了崩溃报告。
这也意味着我必须确认崩溃已修复的唯一当前方法是发布应用程序并等待崩溃报告出现(坏消息)或不出现(真高兴!)。

崩溃报告摘录:

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]];
}

我想我错过了显而易见的事情,所以我不会只是回答“呃,你是对的”,我会接受可能修复错误的答案,指出可以使这段代码更安全和改进的方法。

4

1 回答 1

1

我在做类似的事情时遇到了类似的崩溃,这就是我所做的:

  • 在使用它们之前对每个 CFStringRef 进行空检查 - 这里:cfFirstName、cfLastName
  • 对返回的每个 ABMultiValueRef 进行空检查 - 这里:addressMultiValue

等等。要点是在桥接宏之前执行空值检查。

这是为了处理没有填充某些字段的电话簿记录,就是这样。

于 2013-02-01T22:01:50.167 回答