ios - 如何判断一个对象是否已经被释放

标签 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;
}

最佳答案

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

在 Xcode 中,选择 编辑->重构->转换为 Objective-C ARC

您也可以在线找到教程,但总体来说非常简单。

关于ios - 如何判断一个对象是否已经被释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18306076/

相关文章:

c - 浏览结构数组

ios - 自定义标记图标 iOS 质量差

ios - NSDate 不刷新

objective-c - 如何向 Xcode 添加 #ifdef DEBUG?

IOS block 不保留 self ?

iphone - NSUserDefaults 写入磁盘?

iphone - 局部变量的ARC生命周期,过早释放

c++ - 在 POD 数组上使用 delete 而不是 delete[] 是否安全?

ios - 转换到后台时 NSTimer 不可靠

ios - 在IOS Realm 主键中,如何更新数据,如果已经存在,则不应花费两次?