我正在尝试使用通讯录做一个关于联系人APP的程序,它工作正常,但是当我分析有几个内存泄漏时,我设法最大限度地减少内存泄漏,现在我下降到2个主要内存泄漏的警告,一个在我的地址簿重装功能,我已经包含的意见,看看什么所有的事情我都试过解决Xcode中的内存泄漏问题
-(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;
[email protected]"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循环语句,它说:
非常类似于这个问题:http://stackoverflow.com/questions/18010276/potential-memory-leak-abaddressbookcopyarrayofallpeople – borrrden
当你说“分配的对象没有保留在这个执行路径中[sic]”,你是指“引用”还是“释放”?如果您已经拥有+1,那么没有理由建议保留它。 (更一般地,请复制并粘贴问题导航器中的警告,而不是尝试重新键入它们。) –
“如果使用此格式,我的内存泄漏问题在此解决,但我在程序中遇到错误”什么错误? –