【发布时间】:2013-08-20 19:03:15
【问题描述】:
如何判断一个对象是否已被释放?
如果 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吗?
-
表格视图单元格在滚出屏幕时不会被释放,它会被放入重用队列。您确实需要发布更多代码,很难说出您要做什么。
-
您能否显示您为
kkcell赋值的代码部分(即kkcell = ...;)?在 sn-p 中,您正在尝试使用它而不首先为其分配任何东西。这几乎总是会崩溃。 -
这里没有足够的信息,但我的第一个猜测是您从未将
AVAudioPlayer委托属性设置为nil。为了向后兼容,AVAudioPlayer委托属性设置为assign(notweak),因此如果您的对象在AVAudioPlayer之前被释放,则需要手动将其设置为nil对象是,否则你会有一个悬空指针。 -
您可以做的一个技巧是为您的单元类定义 dealloc 方法。如果您使用 ARC,则没有 [super dealloc];调用你重载的dealloc。这将让您看到您的单元格何时被释放。您还可以在 dealloc 中输出标识哪个单元格的信息,这可能会有所帮助。顺便说一句,如果您在 kkcell 上对 nil 的测试并不能防止出现问题,那么您的参考很有可能是悬空的。
标签: ios objective-c memory-management uitableview