views:

129

answers:

1

Hello everyone,

I have following codes, which have memory leak on device, could you please kindly help check it? Thanks.

@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:&error]; if (!success) { debugLog(@"Unhandled error performing fetch: %@", [error localizedDescription]); NSAssert1(0, @"Unhandled error performing fetch: %@", [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:nil cacheName:@"NewsCache"];

}

return fetchedResultsController; }

 - (void)handleSaveNotification:(NSNotification *)aNotification {

DemoAppDelegate *appDelegate = (DemoAppDelegate *)[UIApplication sharedApplication].delegate; [appDelegate.managedObjectContext mergeChangesFromContextDidSaveNotification:aNotification]; [self fetch]; }

 - (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    News *news = [fetchedResultsController objectAtIndexPath:indexPath];

    // fill cell.label.text according to the news field value

 }   

 @end
+1  A: 

It is astronomically unlikely that setting the delegate or dataSource property of a UITableView instance could cause a noticeable memory leak.

You should examine your surrounding code more thoroughly.

Ryan
@Eiko, Ryan, thanks for your reply, I added more detail code, I used core data to provide data for cell in tableView, I have no idea why the leak occurred. Could you please help check it? Thanks in advance!
mobguang
More code is good, but there still isn't enough. You are releasing and retaining everything appropriately. As Eiko asked previously: what makes you think that this leaks? Have you used Instruments to check it?
Ryan
@Ryan, yes, the instrument indicates that there is leak. But the responsible lib is UIKit instead of my app, can I ignore it? Thanks.
mobguang