2010-03-07 48 views
0

我有一個tableView:didSelectRowAtIndexPath:我每次創建一個ViewController實例選擇一個項目。有時它很長時間都能正常工作,有時它會很快崩潰並伴隨着EXC_BAD_ACCESS。調試器在QuestionDetailViewController中歸咎於行[super dealloc];Popping查看崩潰應用程序在[超dealloc]

爲什麼?我記錄了QuestionDetailViewController的retainCount。這看起來很好。

QuestionDetailViewController

#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]是難以置信的難得的事情,需要來做。

+0

我的代碼中的某處我有[發件人發佈],什麼意思是[自我釋放] – vikingosegundo 2010-03-08 21:56:01

1

相反的:

[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; 
+0

謝謝,但不幸的是它沒有幫助。任何其他建議? – vikingosegundo 2010-03-07 20:19:22