2
votes

I have a MapView in an UIViewController. When I try to load the view after its viewDidUnload it crashes with this message:

-[CALayer release]: message sent to deallocated instance 0x29aea0

I think I do all the necessary things that I should do i the viewDidUnload:

- (void)viewDidUnload {

    [super viewDidUnload];

    locationManager.delegate = nil;
    [locationManager release];
    locationManager = nil;

    mapView.delegate = nil;
    [mapView release];
    mapView = nil;
}

My MapView is in an UIView configured in a xib-file. My VC is never deallocated.

I have been googling around for a while now but I can't find the answer.

Edit

 - (void)viewDidLoad {

    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];

    [label setFont:[UIFont fontWithName:@"Sansation" size:28]];
    [label setShadowOffset:CGSizeMake(0, 1)];
    [label setShadowColor:[UIColor grayColor]];
    self.navigationItem.titleView = labelView;
    [label release];
    [labelView release];

    UIBarButtonItem *checkInButton = [[UIBarButtonItem alloc] initWithTitle:@"Checka In" style:UIBarButtonItemStylePlain target:self action:@selector(checkIn)];
    [[self navigationItem] setRightBarButtonItem:checkInButton];
    [checkInButton release];

    UIBarButtonItem *clueListButton = [[UIBarButtonItem alloc] initWithTitle:@"Ledtrådar" style:UIBarButtonItemStylePlain target:self action:@selector(cluesDown)];
    [[self navigationItem] setLeftBarButtonItem:clueListButton];
    [clueListButton release];

    UINavigationBar *bar = [self.navigationController navigationBar]; 
    [bar setTintColor:[UIColor colorWithRed:0.06 green:0.58 blue:0.88 alpha:1]]; 
}

"labelView" and "label" are IBOutlets.

2
Are the locationManager and mapView actually really retained ? I imagine that they're just added as subviews without having been retained, and then [super viewDidUnload]; might release them and your releasing would cause the crash. - DarkDust
locationManager = [[CLLocationManager alloc] init]; and @property (nonatomic, retain) IBOutlet MKMapView *mapView;. Is this the right way? And if [super viewDidUnload] is releasing the objects I should get an exc_bad_access in the viewDidUnload? The error comes after viewDidAppear but before you can actually see the mapView (just the grid). - Erik Nolander
You don't necessarily get a bad access exception when accessing a deallocated object. You can also either get the message sent to deallocated instance error or unrecognized selector if a new object now lives at that memory location. But you now say that the message comes after viewDidAppear, so you should show us this method instead. The viewDidUnload method is called when the view controller is releasing its views (cleanup). - DarkDust
There is nothing written in the viewDidAppear-method. I just had a breakpoint there to see if it was called. I will edit my question and add the viewDidLoad-method. - Erik Nolander

2 Answers

7
votes

Since you have neither allocated nor retained label and labelView in your viewDidLoad method you may not release them here (you are overreleasing them).

0
votes

Because you did some unwanted releases, like the class method return value. you can release all you retrain object in -(void)dealloc