简体   繁体   中英

How to tell if an object has been released

How can I tell if an object has been released?

If the UITableViewCell has been moved out screen, kkcell object will be autoreleased by UITableView.

When the audioPlayer finishes, program calls " [kkcell stopSpeakAmination]", but this causes the program to crash.

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

    [kkcell stopSpeakAmination];
    playing=NO;
}

I also use

if(kkcell)

or

if(kkcell!=nil)

but it still crashes.

Here is some more code that I am using:

//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;
}

Convert your project to ARC. It is much easier and automated than you would think. I've converted about 3 real life projects that were quite large in mid-development and never had any issues (put aside one hour to do it, you probably won't need that much time and its definitely worth it)

in Xcode, select edit->refactor->convert to Objective-C ARC

You can also find tutorials online but its pretty straight forward overall.

Your program crashes most likely because the object was indeed fully released but that doesn't mean that kkcell would be set to nil , hence your checks failing, kkcell is "dangling".

To answer your specific question, one way to test for release is to override dealloc as in

-(void)dealloc
{
    NSLog(@"deallocated");
    [super dealloc];
}

This may help you troubleshoot by telling you exactly when the final release happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM