我正在尝试使用通讯录在联系人 APP 上做一个程序,它工作正常,但是当我分析有几个内存泄漏时,我已经设法将内存泄漏降到最低,现在我只收到 2 个主内存泄漏警告,一个在我的地址书籍重新加载功能,我已经包含评论以显示我尝试过的所有事情
-(void)reloadAddressBook
{
//if(self.addressBook)
//CFRelease(self.addressBook);
self.addressBook = (__bridge ABAddressBookRef) CFBridgingRelease(ABAddressBookCreate());
if(ABAddressBookHasUnsavedChanges(self.addressBook))
{
ABAddressBookSave(self.addressBook,NULL);
}
//if(self.contactAdd)
//CFRelease(self.contactAdd);
self.contactAdd= ABAddressBookCopyArrayOfAllPeople(self.addressBook);
**//Memory warning here and says: call to function ABAddressBookCopyArrayOfAllPeople returns a core foundation object with a +1 retain count**
//self.contactAdd= (__bridge ABAddressBookRef) CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(self.addressBook));
**// If I use this format my memory leak issue solves here but I get error in my program**
}
- (void)viewDidLoad
{**//Memory warning here and says :object leaked :allocated object is not retained lated in this execution path and has retain count +1**
[super viewDidLoad];
self.contactSearchBar.delegate=self;
self.contactTableView.delegate=self;
self.contactTableView.dataSource=self;
UIBarButtonItem *addContactButton=[[UIBarButtonItem alloc]initWithTitle:@"Add" style:UIBarButtonItemStyleBordered target:self action:@selector(newContact:)];
self.navigationItem.rightBarButtonItem=addContactButton;
self.navigationItem.title=@"My Contacts";
}
另一个内存泄漏是在这个搜索栏功能中
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length==0)
{
isFiltered=NO;
}
else
{
isFiltered=YES;
int j=0,i=0;
self.filteredData= CFArrayCreateMutable(kCFAllocatorDefault, 0,&kCFTypeArrayCallBacks);**// Memory warning here and says: call to function CFArrayCreateMutable returns a core foundation object with a +1 retain count**
for(i=0;i<CFArrayGetCount(self.contactAdd);i++)**//Memory warning here and says :object leaked :allocated object is not retained lated in this execution path and has retain count +1**
{
self.person=CFArrayGetValueAtIndex(self.contactAdd,i);
NSString *str=[[NSString stringWithFormat:@"%@", (__bridge_transfer NSString *)ABRecordCopyValue(self.person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSRange contactRange= [str rangeOfString: searchText options:NSCaseInsensitiveSearch];
NSLog(@"i=%d, j=%d",i,j);
if(contactRange.location!=NSNotFound)
{
CFArrayInsertValueAtIndex(self.filteredData,j++,self.person);
CFArrayGetValueAtIndex(self.filteredData,j-1);
}
}
//CFRelease(self.contactAdd);
//CFRelease(self.filteredData);
}
for循环语句上显示内存泄漏,它说: