弹出视图在 [super dealloc] 处崩溃应用程序

Posted

技术标签:

【中文标题】弹出视图在 [super dealloc] 处崩溃应用程序【英文标题】:Popping View crashes Application at [super dealloc] 【发布时间】:2010-03-07 19:50:54 【问题描述】:

我有一个tableView:didSelectRowAtIndexPath:,每次选择一个项目时我都会在其中创建一个 ViewController-Instance。有时它可以正常工作很长时间,有时它会很快崩溃EXC_BAD_ACCESS。调试器将问题归咎于 QuestionDetailViewController 中的 [super dealloc]; 行。

为什么?我记录了 QuestionDetailViewController retainCount。看起来不错。

问题详细视图控制器

#import <UIKit/UIKit.h>
#import "Question.h"
#import "Answer.h"
@interface QuestionDetailViewController : UIViewController < UIScrollViewDelegate , QuestionDetailViewProtocol> 
    Question *question;
    UILabel *answerText;
    UILabel *titleLabel;


@property(nonatomic,retain) Question *question;
@property(nonatomic,retain) UILabel *answerText;
@property(nonatomic,retain) UILabel *titleLabel;

@end


#import "QuestionDetailViewController.h"
@implementation QuestionDetailViewController
@synthesize question;
@synthesize answerText;
@synthesize titleLabel; 


- (void)viewDidLoad 
    [super viewDidLoad];


- (void)viewWillAppear:(BOOL)animated 
    UIColor *bgColor = [UIColor colorWithRed:199.0/255 green:234.0/255 blue:251.0/255 alpha:1];
    self.answerText = (UILabel *)[self.view viewWithTag:2]; 
    self.answerText.backgroundColor = bgColor;
    self.answerText.text = self.question.answer.text;
    self.titleLabel = (UILabel*)[self.view viewWithTag:1]; 
    CGSize sizeTitle = [self.question.title sizeWithFont:[titleLabel font]
                                       constrainedToSize:CGSizeMake(280.0, INFINITY) 
                                           lineBreakMode:UILineBreakModeWordWrap];
    self.titleLabel.text = self.question.title;
    self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    self.titleLabel.frame = CGRectMake(10,10, 260, sizeTitle.height);

    CGSize sizeText = [self.question.answer.text sizeWithFont:[self.answerText font]
                                            constrainedToSize:CGSizeMake(280.0, INFINITY) 
                                                lineBreakMode:UILineBreakModeWordWrap];
    self.answerText.frame = CGRectMake(0,20+sizeTitle.height, 310, sizeText.height+sizeTitle.height);
    [(UIScrollView *)self.view setContentSize:CGSizeMake(280, answerText.frame.size.height +sizeTitle.height)];
    self.view.backgroundColor = bgColor;
    [super viewWillAppear:animated];


- (void)viewDidDisappear:(BOOL)animated 
    NSLog(@"%d", [self retainCount]);
    [super viewDidDisappear:animated];
    [self release];


-(IBAction)goBack:(id)sender
    [self.navigationController popViewControllerAnimated:YES];


- (void)dealloc 
    NSLog(@"QDC dealoc");
    [self.answerText release];
    [self.titleLabel release];
    [self.question release];
    [super dealloc];


@end

tableView:didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath    
    QuestionDetailViewController *qdc = [[QuestionDetailViewController alloc] initWithNibName:@"QuestionDetailViewController" bundle:nil];
    Question* question;
    if ([filteredQuestions count]>0) 
        question = [self.filteredQuestions objectAtIndex:indexPath.row];
     else 
        question = [self.questions objectAtIndex:indexPath.row];
    
    [qdc setQuestion:question];
    [question release];
    NSLog(@"%d", [qdc retainCount]);
    [self.navigationController pushViewController:qdc animated:YES];
    [qdc release];

【问题讨论】:

【参考方案1】:

这个:

- (void)viewDidDisappear:(BOOL)animated 
    NSLog(@"%d", [self retainCount]);
    [super viewDidDisappear:animated];
    [self release];

看起来非常可疑。你做了[self retain]吗?如果不是,那你为什么要释放?如果你是,你可能做错了什么,因为[self retain] 是一件非常罕见的事情,需要去做。

【讨论】:

在我的代码中某处我有 [sender release],这意味着什么是 [self release]【参考方案2】:

代替:

[self.answerText release];
[self.titleLabel release];
[self.question release];

使用:

self.answerText = nil;
self.titleLabel = nil;
self.question = nil;

或:

[answerText release]; answerText = nil;
[titleLabel release]; titleLabel = nil;
[question release]; question = nil;

【讨论】:

谢谢,但不幸的是它没有帮助。还有其他建议吗?

以上是关于弹出视图在 [super dealloc] 处崩溃应用程序的主要内容,如果未能解决你的问题,请参考以下文章

为啥我必须最后调用 super -dealloc,而不是先调用?

WKWebView 子类在 dealloc 上崩溃

dealloc时取weakself引起崩溃

iOS dealloc中初始化weak指针崩溃防护

UIWebView 发布导致崩溃

为啥在另一个线程上发生 dealloc 时尝试创建对 self 的弱引用时会崩溃?