将nil设置为IBOutlet时的iPhone - EXC_BAD_ACCESS - 内存管理

时间:2012-01-12 15:04:28

标签: iphone memory-management exc-bad-access null iboutlet

我正面临一个烦人的问题,我找不到原因。

我有一个UIViewController我以模态出现:

interviewsViewController *interviewsVC = [[interviewsViewController alloc] initWithNibName:nil bundle:nil];
[interviewsVC setManagedObjectContext:_managedObjectContext];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:interviewsVC];
[interviewsVC release];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];

然后当我解除视图控制器时:

- (void)dismissViewController
{
     [self dismissModalViewControllerAnimated:YES];
}

dealloc被调用:

- (void)dealloc
{
    [_managedObjectContext release];
    [_interviewsArray release];
    [scrollView release];
    [pageControl release];
}

取消视图控制器后,我通过iPhone模拟器菜单发送内存警告,并调用viewdidunload方法:

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.scrollView = nil;
    self.pageControl = nil;
}

但是self.scrollView = nil上总是出现错误EXC_BAD_ACCES ...更具体地说,就在这一行:

@synthesize scrollView;

我无法找出原因?

如果我在这一行上方的行上添加断点,则scrollView不是僵尸或等于0x0 ......

你有什么想法吗?

PS:这是标题:

#import <UIKit/UIKit.h>

@interface interviewsViewController : UIViewController <UIScrollViewDelegate>
{
    NSManagedObjectContext *_managedObjectContext;

    NSMutableArray *_interviewsArray;

    NSUInteger _fetchOffset;

    CGFloat _lastXValue;
}

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *interviewsArray;

//IBOutlet
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;

我在XIB中设置了scrollview的委托(不在代码中)。

1 个答案:

答案 0 :(得分:1)

您需要在dealloc中正确发布 -

使用 -

- (void)dealloc {
      [_managedObjectContext release];
      [_interviewsArray release];
      self.scrollView = nil;
      self.pageControl = nil;

      [super dealloc];
}

还可以使用ViewDidUnload,因为在内存不足警告的情况下,这将有所帮助。