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

发布于 2024-12-26 14:09:51 字数 1948 浏览 1 评论 0原文

我面临着一个恼人的问题,但我找不到原因。

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡 2025-01-02 14:09:51

您需要在 dealloc-

Use

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

      [super dealloc];
}

-ViewDidUnload 中正确释放,因为这在出现内存不足警告时会很有帮助。

You need to release properly in dealloc-

Use-

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

      [super dealloc];
}

ViewDidUnload also be used as that will be helpfull in case of low memory warnings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文