当我使用CFRelease时应用程序崩溃

时间:2013-11-19 08:54:40

标签: ios iphone xcode

我想了解我应该如何使用CFRelease。我想我没用得对。我试图发布时应用程序崩溃。

            if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")){
            //get phone numbers
            ABMultiValueRef emails = ABRecordCopyValue((__bridge ABRecordRef)person,kABPersonEmailProperty);
            for(CFIndex i=0;i<ABMultiValueGetCount(emails);++i) {
                CFStringRef mEmailRef = ABMultiValueCopyValueAtIndex(emails, i);
                CFStringRef mEmailTypeRef = ABMultiValueCopyLabelAtIndex(emails, i);
                NSString * type = (__bridge NSString*) mEmailTypeRef;
                NSString * email = (__bridge NSString*) mEmailRef;
                NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"!$_><"];
                type = [[type componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
                if (!email || [email length]==0 || !type || [type length]==0) {

                }else
                    [GlobalFunctions addValue:email andKey:CONTACT_EMAIL toGroup:type inArray:array];

                if (mEmailRef) {
                    CFRelease(mEmailRef);
                }
                if (mEmailTypeRef) {
                    CFRelease(mEmailTypeRef); // crash
                }
                if (emails) {
                    CFRelease(emails);
                }

            }


        }

enter image description here

enter image description here

编辑:

我发现了问题,电子邮件对象在for循环中发布但之前创建。

1 个答案:

答案 0 :(得分:4)

每次使用 for循环发布电子邮件时,您的代码都会崩溃。在for循环之外写下这个条件。

if (emails) {
   CFRelease(emails);
}

而且我也想知道你在控制台中遇到了什么错误。