[英]EXC_BAD_ACCESS (code=1) Error in Xcode
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!
在目标c中编程大约3周,所有这些管理内存的东西都令人困惑! What is basically happening is that i have this mapview in a tableview.
基本上发生的是我在tableview中有这个mapview。 When clicking the back button to leave the mapview and return to the main menu i get the error above.
单击后退按钮离开mapview并返回主菜单时,我得到上面的错误。 Here is the code from the Header file
这是Header文件中的代码
#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? 有人可以为我揭开光明吗? :)
:)
[super dealloc];
must be the last call in dealloc
必须是
dealloc
的最后一次调用
also after [mapView release];
也是在
[mapView release];
mapView might be gone already. mapView可能已经消失了。
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
}
此错误也可能由不兼容的API引起(例如,您为iOS 6.0构建,但使用仅在iOS> = 8.2中引入的方法)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.