首页 > 代码库 > autoreleasepool

autoreleasepool

@autoreleasepool{}

ARC模式下:

1.[p autorelease]; 只会将p放入pool中,不会立即release(即 retain - 1),延迟至pool销毁时。

2.在pool销毁时,其中的所有对象都将进行release。

3.若开辟多线程时(如:[self performSelectorInBackground:@selector(setupLocalContactsData:) withObject:success]),

   要在setupLocalContactsData:中加入autoreleasepool进行内存管理。

EX:

  if (version < 6.0) {
            _addressBookRef = ABAddressBookCreate();
            [self performSelectorInBackground:@selector(setupLocalContactsData:) withObject:success];//新建后台线程执行
         
        }

 

- (void)setupLocalContactsData:(void (^)(NSArray *localContacts))success
{
    @autoreleasepool { //对新线程做内存管理
        
        NSMutableDictionary *pinyinDictionary = nil;
        NSMutableArray *resultArray = [NSMutableArray array];
        
        CFArrayRef allPeopleArrayRef = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(_addressBookRef, nil, kABPersonSortByLastName);

    }

}

 

autoreleasepool