消息已发送到解除分配的实例错误

时间:2011-06-06 19:18:15

标签: iphone objective-c xcode memory-management dealloc

我在行*** -[CFString release]: message sent to deallocated instance 0x7021e80的dealloc方法中遇到[muscleURL release];的崩溃事件

muscleURL的初始值为@property (nonatomic, retain) NSString *muscleURL;

仅当我单击导航栏中的完成按钮时才会发生这种情况。以下是相关代码:

- (void)viewDidLoad
{
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView)];
    [self.navigationItem setRightBarButtonItem:doneButton];
    [doneButton release];
}

-(void)dismissView
{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] -3)] animated:YES];
}

编辑:

在父视图中,父视图的muscleURL就像这样

-(void)didSelectRowAtIndexPath
{
    NSString *muscleURL = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:@"musclePicture"];
    detailViewController.muscleURL = muscleURL;
}

2 个答案:

答案 0 :(得分:0)

试试这个。

- (void)viewDidLoad
{
UIBarButtonItem * doneButton = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView)]autorelease]; 
[self.navigationItem setRightBarButtonItem:doneButton];

}

-(void)didSelectRowAtIndexPath
{
NSString *tempURL = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:@"musclePicture"];
detailViewController.muscleURL = tempURL;
}

答案 1 :(得分:0)

    NSString *muscleURL = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:@"musclePicture"];

这将返回一个自动释放的对象,因此您不需要在dealloc方法中释放它,因为系统已经释放了内存。阅读Objective-C内存管理可能是一个好主意,Apple开发人员网站上有一个文档。