我正在嘗試使用通訊錄做一個關於聯繫人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,那麼沒有理由建議保留它。 (更一般地,請複製並粘貼問題導航器中的警告,而不是嘗試重新鍵入它們。) –
「如果使用此格式,我的內存泄漏問題在此解決,但我在程序中遇到錯誤」什麼錯誤? –