Search code examples
iphoneobjective-cxcodememory-managementdealloc

Message Sent To Deallocated Instance Error


I am getting a crash saying *** -[CFString release]: message sent to deallocated instance 0x7021e80 in my dealloc method for line [muscleURL release];

The init for muscleURL is @property (nonatomic, retain) NSString *muscleURL;

This only happens when I click the done button in my NavBar. Here is the related code:

- (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];
}

Edit:

In the parent view, parent view's muscleURL is init like this

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

Solution

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

    This returns an auto released object so you do not need to release it in your dealloc method as the system has already deallocated the memory. It might be a good idea for you to read up on Objective-C memory management, there is a document on the Apple Developer website.