开发者

pushViewController memory leak

开发者 https://www.devze.com 2023-01-15 09:10 出处:网络
I have following code, instrument indicate that the pushViewController method has 32 bytes memory leak on device. Could you please kindly help check what rule I break? Should I change some \"retain\"

I have following code, instrument indicate that the pushViewController method has 32 bytes memory leak on device. Could you please kindly help check what rule I break? Should I change some "retain" to "assign" for declaration? Thanks in advance!

@interface GuideNewsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {

@private
    NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
    UITableView *tableView;
    NewsListViewController *newsListViewController;
}

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain, readonly) NewsListViewController *newsListViewController;



@implementation GuideNewsViewController
......
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  Member *member = [fetchedResultsController objectAtIndexPath:indexPath];
  self.newsListViewController.managedObjectContext = self.managedObjectContext;
  self.newsListViewController.title = member.memberName;
  self.newsListViewController.author = member;
   **// leak here**
  [self.navigationController pushViewController:self.newsListViewController animated:YES];
}
......
@end


@interface N开发者_如何学GoewsListViewController :  UIViewController <UITableViewDataSource, UITableViewDelegate> {

@private
    NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
    UITableView *tableView;
    Member *author;
}

@property (nonatomic, assign) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign, readonly) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, assign) Member *author;

@end


Just a guess but, what happen if you change your newsListViewController from retain to assign? I barely write retain, readonly together.


When does self.newsListViewController be released ? I don't think that point causes problem.

Generally, I pushViewController with following manner.

MySelfViewController *childView = [[MySelfViewController alloc] init]; 

// set up necessary properties of childView
...

// navigationController will release childView when it pops view controller.
[self pushViewController:childView animated:YES];

// release childView after pushViewController  
[childView release];  

If childView have to notify self something happened, it can use delegate to notify self.


Edit 1:

An example is as following.

// MySelfViewController.h
@protocol MySelfProtocol <NSObject>
- (void)notifySomethingHappened;
@end

@interface MySelfViewController : UIViewController {
    id <MySelfProtocol> _delegate;
}

/// client init childview by pass self as parameter.
/// ex: Inside view controller A, he calls by 
/// childView = [[MySelfViewController alloc] initWithDelegate:self];
- (id)initWithDelegate:(id)delegate;

/// other member methods
@end

// MySelfViewController.m
@implement MySelfViewController
- (id)initWithDelegate:(id)delegate
{
  if (self = [super init])
  {
      /// assign policy. 
      /// childView should not retain parent view or delegate.
      /// It is possible to let delegate never run dealloc.
      _delegate = delegate;

      /// custom initialization
      .... 

  }
  return self;
}

- (void)someThingHappen
{
   [_delegate notifySomethingHappend];
}
@end
0

精彩评论

暂无评论...
验证码 换一张
取 消