EXC_BAD_ACCESS(code = 1)Xcode中的错误

问题描述:

我知道这个错误与内存管理有关,但我必须承认我很难过!在目标c编程约3周,所有这些管理内存的东西都令人困惑! 基本上发生的是我在tableview中有这个mapview。当点击后退按钮离开地图视图并返回到主菜单时,我会看到上面的错误。 这里是头文件EXC_BAD_ACCESS(code = 1)Xcode中的错误

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 

@interface MapViewController : UIViewController <MKMapViewDelegate> { 

    IBOutlet MKMapView* mapView; 
    BOOL locate; 

} 

@property (nonatomic, retain) IBOutlet MKMapView* mapView; 

@end 

和执行文件

#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 

任何人能提供一些线索,我的代码? :)

+0

我试过评论出各种各样的代码行,看看我是否可以解决它,但每次都是同样的事情,正如我所说,我对这个很陌生,所以任何帮助,即使有调试,都将不胜感激:)(对不起,无言以对在背部疼痛lol) – Craig

[super dealloc];也必须经过[mapView release]; MapView的可能已经过去了在dealloc

最后一次通话。

尝试

- (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 
} 
+0

仍然崩溃与同样的错误,我很害怕:( – Craig

+1

你可以给完整的错误信息? – vikingosegundo

+0

EXC_BAD_AC CESS(代码= 1,地址= 0xa0e69c14),但内存地址每次都会更改。只是编辑,以增加一些清晰度,因为我忘了提及应用程序崩溃时>。 Craig

此错误也可以引起不兼容的API(例如,你建立的iOS 6.0,但使用的方法只在iOS中介绍> = 8.2)

+1

这不是一个答案,更像是一个评论。 – Zippy

+0

我没有足够的声望发表评论,让它成为) – schmidt9