如何判断对象是否已被释放

时间:2013-08-19 03:39:09

标签: ios objective-c memory-management uitableview

如何判断对象是否已被释放?

如果UITableViewCell已移出屏幕,则kkcell对象将由UITableView自动释放。

当audioPlayer完成时,程序调用“[kkcell stopSpeakAmination]”,但这会导致程序崩溃。

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{

    [kkcell stopSpeakAmination];
    playing=NO;
}

我也用

if(kkcell)

if(kkcell!=nil)

但它仍然崩溃。

以下是我正在使用的更多代码:

//KKMessageCell2.m
....
@property(nonatomic, retain) UIImageView *SenderVoiceNodePlaying;
@property(nonatomic, retain) UIImageView *ReceiverVoiceNodePlaying;
....

-(void)stopSpeakAmination{
    [self.SenderVoiceNodePlaying stopAnimating];
    [self.ReceiverVoiceNodePlaying stopAnimating];
}

-(void)speakAminationOnRight:(UIButton*)btn{

    //rightside
    NSArray *speekImageAry = [[NSArray alloc]initWithObjects:
    [UIImage imageNamed:@"SenderVoiceNodePlaying001"],
    [UIImage imageNamed:@"SenderVoiceNodePlaying002"],
                          [UIImage imageNamed:@"SenderVoiceNodePlaying003"], nil];


    self.SenderVoiceNodePlaying.animationImages = speekImageAry; 
    self.SenderVoiceNodePlaying.animationDuration = 1.0; 
    self.SenderVoiceNodePlaying.animationRepeatCount = 0; 
    [self.SenderVoiceNodePlaying startAnimating];

    NSString *fileName=btn.titleLabel.text;
    fileName=[fileName substringFromIndex:10];
    KKMessageCell2 *cell=(KKMessageCell2*)[btn superview];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"nPlayAmr"
                                    object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
                                                         fileName,@"fileName",
                                                         cell,@"cell",nil]];
}

//KKMessageCell2.m
....

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"msgCell";
    NSDictionary *dict = [messages objectAtIndex:indexPath.row];
    KKMessageCell2 *cell = [[[KKMessageCell2 alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setCellView:dict andMy_avatar_url:self.my_avatar_url];
    [cell.avatarLeftImageButton addTarget:self action:@selector(pushToUserDetail:) forControlEvents:UIControlEventTouchUpInside];
    [cell.avatarRightImageButton addTarget:self action:@selector(pushToUserDetail:) forControlEvents:UIControlEventTouchUpInside];
    [cell.chatphoto addTarget:self action:@selector(photoClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;

}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{

    [kkcell stopSpeakAmination];
    playing=NO;
}

2 个答案:

答案 0 :(得分:1)

将您的项目转换为ARC。它比您想象的更容易和自动化。我已经转换了大约3个在开发中期相当大的现实生活项目并且没有任何问题(搁置一个小时去做,你可能不需要那么多时间而且绝对值得)

在Xcode中,选择 edit-> refactor->转换为Objective-C ARC

您也可以在线查找教程,但整体上非常简单。

答案 1 :(得分:0)

您的程序很可能崩溃,因为该对象确实已完全释放,但这并不意味着kkcell将设置为nil,因此您的检查失败,kkcell是“悬空”

要回答您的具体问题,测试发布的一种方法是覆盖{/ 1}},如

dealloc

这可以通过告诉您最终发布的确切时间来帮助您排除故障。