我有以下代码,设备上有内存泄漏,请您帮忙检查一下?感谢。
@interface NewsListViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {
@private
UITableView *tableView;
NSFetchedResultsController *fetchedResultsController;
......
}
@property (nonatomic, retain, readonly) NSFetchedResultsController *fetchedResultsController;
@end
@implementation NewsListViewController {
......
- (void)dealloc {
[fetchedResultsController release];
fetchedResultsController = nil;
tableView.delegate = nil;
tableView.dataSource = nil;
[tableView release];
tableView = nil;
[super dealloc];
}
-(void)viewDidLoad {
......
tableView.delegate = self; // **leak here**
tableView.dataSource = self; // **leak here**
DemoAppDelegate *appDelegate = (DemoAppDelegate *)[UIApplication sharedApplication].delegate;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleSaveNotification:)
name:NSManagedObjectContextDidSaveNotification
object:appDelegate.managedObjectContext];
[self fetch];
}
- (void)fetch {
NSError * error = nil; BOOL success = [self.fetchedResultsController performFetch:&amp; error]; if(!success){ debugLog(@“执行获取的未处理错误:%@”,[error localizedDescription]); NSAssert1(0,@“执行获取的未处理错误:%@”,[error localizedDescription]); } [tableView reloadData];
}
- (NSFetchedResultsController *)fetchedResultsController {
if(fetchedResultsController == nil){ NSFetchRequest * fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
DemoAppDelegate * appDelegate =(DemoAppDelegate *)[UIApplication sharedApplication] .delegate; [fetchRequest setEntity:[NSEntityDescription entityForName:@“News” inManagedObjectContext:appDelegate.managedObjectContext]];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:无 cacheName:@ “NewsCache”];
}
return fetchedResultsController; }
- (void)handleSaveNotification:(NSNotification *)aNotification {
DemoAppDelegate * appDelegate =(DemoAppDelegate *)[UIApplication sharedApplication] .delegate; [appDelegate.managedObjectContext mergeChangesFromContextDidSaveNotification:aNotification]; [取自]; }
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
News *news = [fetchedResultsController objectAtIndexPath:indexPath];
// fill cell.label.text according to the news field value
}
@end
答案 0 :(得分:1)
天文数据不太可能设置UITableView实例的delegate或dataSource属性可能会导致明显的内存泄漏。
您应该更彻底地检查周围的代码。