EXC_BAD_ACCESS (code=1) Error in Xcode

30,146

[super dealloc]; must be the last call in dealloc

also after [mapView release]; mapView might be gone already.

try

- (void)dealloc {

    [self.mapView.userLocation removeObserver:self forKeyPath:@"location"];
    [self.mapView removeFromSuperview];
    [mapView release];
    self.mapView = nil; 
    [super dealloc];  // at this point self — that is the same object as super — is not existent anymore
}
Share:
30,146
Craig
Author by

Craig

Recently got a job as a trainee learning Objective-C in Xcode. Have experience in a few other bits and bobs so will help where i can, but for now i think i'll mostly need help :P

Updated on February 03, 2020

Comments

  • Craig
    Craig over 4 years

    I know this error has something to do with memory management but i must admit i'm stumped! Been programming in objective c for about 3 weeks and all this managing memory stuff is confusing! What is basically happening is that i have this mapview in a tableview. When clicking the back button to leave the mapview and return to the main menu i get the error above. Here is the code from the Header file

    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>
    
    @interface MapViewController : UIViewController <MKMapViewDelegate> {
    
        IBOutlet MKMapView* mapView;
        BOOL locate;
    
    }
    
    @property (nonatomic, retain) IBOutlet MKMapView* mapView;
    
    @end
    

    and the Implementation file

    #import "MapViewController.h"
    
    @implementation MapViewController
    
    @synthesize mapView;
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
        mapView.delegate=self;
        mapView.showsUserLocation = YES;
    
        [self.view addSubview:mapView];
    
        [self.mapView.userLocation addObserver:self
                                    forKeyPath:@"location"
                                       options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
                                       context:nil];
        locate = YES;
    
    }
    
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
    
        if (locate == YES) {
        MKCoordinateRegion region;
        region.center = self.mapView.userLocation.coordinate;
    
        MKCoordinateSpan span;
        span.latitudeDelta  = 0.1; 
        span.longitudeDelta = 0.1;
        region.span = span;
    
        [self.mapView setRegion:region animated:YES];
            locate = NO;
        }
    
    }
    
    - (void)didReceiveMemoryWarning {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    
        // Release any cached data, images, etc that aren't in use.
    }
    - (void)dealloc {
        [super dealloc];
        [mapView release];
        [self.mapView.userLocation removeObserver:self forKeyPath:@"location"];
        [self.mapView removeFromSuperview];
        self.mapView = nil;
    }
    
    @end
    

    Can anybody shed some light for me? :)

  • Craig
    Craig over 11 years
    EXC_BAD_ACCESS (code=1, address=0xa0e69c14) but the memory address changes each time. Just edited to add some clarity as i forgot to mention when the app is crashing >.<
  • Craig
    Craig over 11 years
    GOT IT! You were right, i commented out mapView release and it is no longer crashing. So it was obviously releasing it then trying to release again. Thanks :) (Says i cant accept this answer for 5 minutes, so bare with me for ya rep mate)
  • Zippy
    Zippy over 8 years
    This is not an answer, more like a comment.
  • schmidt9
    schmidt9 over 8 years
    I have not enough reputation to post comments, let it be )