IOS Modify Address Book contact addresses crash cause analysis

Source: Internet
Author: User

In the current project, the iOS system address book needs to be read, modified operation. At the time of the address modification, there was a strange phenomenon:

If the contact does not have an address field (or a brand new contact), it is possible to modify its address to be successful.

If the person has an address field, the modification will crash when it is done. Console typing:

-[cfstring release]: message sent to deallocated instance 0x81d26f0

This should be a zombie object that repeats the release of an object. First I check the code to modify the Address book, but I do not see the problem, the following is the code

   Set address Abmutablemultivalueref multiaddress = abmultivaluecreatemutable (Kabmultidictionarypropertytype);        For (phonetypepair* p in Contact.addressarr) {//content to determine null if ([P.content length]==0) {continue;        } nsmutabledictionary *addressdictionary = [[Nsmutabledictionary alloc] init]; Write address only to the street field [Addressdictionary setobject:[p.content mutablecopy] Forkey: (NSString *) Kabpersonaddressstreetkey        ];        [Addressdictionary setobject:@ "" Forkey: (NSString *) Kabpersonaddresscitykey];        [Addressdictionary setobject:@ "" Forkey: (NSString *) Kabpersonaddressstatekey];        [Addressdictionary setobject:@ "" Forkey: (NSString *) Kabpersonaddresszipkey];        [Addressdictionary setobject:@ "" Forkey: (NSString *) Kabpersonaddresscountrycodekey]; Put the dictionary in a multivalued object if ([P.type isequaltostring:kaddresstype_work]) {Abmultivalueaddvalueandlabel (multiaddress        , Cfbridgingretain (addressdictionary), Kabworklabel, NULL);}else if ([P.type isequaltostring:kaddresstype_home]) {Abmultivalueaddvalueandlabel (multiaddress, CFBridgingRet        Ain (Addressdictionary), Kabhomelabel, NULL);        }else{Abmultivalueaddvalueandlabel (multiaddress, Cfbridgingretain (addressdictionary), KABOtherLabel, NULL);    }} abrecordsetvalue (person, Kabpersonaddressproperty, multiaddress, NULL);    Abaddressbookaddrecord (addressbook, person, nil);    Abaddressbooksave (AddressBook, NULL);    if (multiaddress) {cfrelease (multiaddress);    }
Program crashes in Abaddressbooksave (AddressBook, NULL); Best of all, Google looked at a lot of information to see if the "multi-value" of the object used wrong, or code order problem. Have no results.

Later, I remember instruments this tool to view zombie objects. Immediately starting profile. The results are as follows:



The place of Zombie is Abcmultivaluedestroy. However, I have noticed Addressbookengine's getaddress: function. Suddenly I realized that the problem should be read when the CF and OC objects were converted. Randomly, I opened the URL and turned to arc description

__bridge only does type conversion, but does not modify the object (memory) management rights;
__bridge_retained (You can also use Cfbridgingretain) to convert objects from Objective-c to core foundation objects while handing over the management of the object (memory) to us, Subsequent use of cfrelease or related methods to release objects;
__bridge_transfer (You can also use cfbridgingrelease) to convert a core foundation object to an Objective-c object while handing over the management of the object (memory) to arc.

Well, the problem should be reading the address: Look at the code.

/** * Get Address * * @param recordref contacts Single Contact Reference * * @return address */-(nsdictionary*) getaddress: (abrecordref) recordref{        1. Determine if (recordref = = nil) {return nil;    } Nsmutablearray *addresshome = [[Nsmutablearray alloc]init];    Nsmutablearray *addresswork = [[Nsmutablearray alloc]init];    Nsmutablearray *other = [[Nsmutablearray alloc]init];    2. Create a dictionary, get a list of multi-key values nsmutabledictionary *multivaluedic = [[Nsmutabledictionary alloc] initwithcapacity:1];    Abmultivalueref Multivaluearr = Abrecordcopyvalue (RecordRef, Kabpersonaddressproperty);    3. Encapsulate multiple values into a dictionary. int count = Multivaluearr?    Abmultivaluegetcount (Multivaluearr): 0;        if (Count > 0) {count = (count <= kmaxaddressnumber?count:kmaxaddressnumber); for (int i = 0; i < count; i++) {@autoreleasepool {//lable//note bridging, convert CF object to OC Object 。 Under Arc, the OC object is automatically released: Reference http://blog.csdn.net/hherima/article/details/16356577 nsstring* label = Cfbridgingrelease (Abmultivaluecopylabelatindex (multivaluearr,i));                Value Cfdictionaryref dict = Abmultivaluecopyvalueatindex (Multivaluearr, i);                nsstring* Street =cfbridgingrelease (Cfdictionarygetvalue (Dict, Kabpersonaddressstreetkey));                nsstring* City =cfbridgingrelease (Cfdictionarygetvalue (Dict, Kabpersonaddresscitykey));                nsstring* Country =cfbridgingrelease (Cfdictionarygetvalue (Dict, Kabpersonaddresscountrykey));                                              Cfrelease (dict);//should be removed nsstring *syntheticaddress = [NSString stringwithformat:@ "%@%@%@" , (street?street:@ ""), (city?city                                : @ ""), (country?country:@ "")]; if (label = = Nil | | [Label isequaltostring:@ "_$!I made a hypothesis before I could find a specific question. If you return from the beginning of this function, if you do not crash, the explanation is the problem of subsequent code. Sure enough

The problem is:

Cfrelease (DICT);

Since I have used the cfbridgingrelease, it is not necessary to releasedict this object. The main is the above code, is copy from the Internet. No change.

IOS Modify Address Book contact addresses crash cause analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.