iPhone - 将 IBOutlet 设置为 nil 时的 EXC_BAD_ACCESS - 内存管理
我面临着一个恼人的问题,但我找不到原因。
我有一个 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;
我找不到原因?
如果我在这一行上面添加一个断点,则滚动视图不是僵尸或等于 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 中设置了滚动视图的委托(而不是在代码中)。
I'm facing an annoying issue and I can't find out why.
I have a UIViewController I present in modal like that :
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];
Then when I dismiss the view controller like this :
- (void)dismissViewController
{
[self dismissModalViewControllerAnimated:YES];
}
The dealloc gets called :
- (void)dealloc
{
[_managedObjectContext release];
[_interviewsArray release];
[scrollView release];
[pageControl release];
}
Once the view controller is dismissed, I send an memory warning via the iPhone Simulator Menu and the viewdidunload method gets called :
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.scrollView = nil;
self.pageControl = nil;
}
But there's always an error EXC_BAD_ACCES on the self.scrollView = nil ... More specifically at this line :
@synthesize scrollView;
And I can't find out why ?
If I add a breakpoint on the line above this one, the scrollView is not a zombie or equal to 0x0 ...
Do you have an idea ?
PS : Here's the header :
#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;
And I set the delegate of the scrollview in the XIB (not in the code).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 dealloc-
Use
-ViewDidUnload 中正确释放,因为这在出现内存不足警告时会很有帮助。
You need to release properly in dealloc-
Use-
ViewDidUnload also be used as that will be helpfull in case of low memory warnings.