2012-08-30 49 views
5

我知道這個錯誤與內存管理有關,但我必須承認我很難過!在目標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

回答

12

[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

0

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

+1

這不是一個答案,更像是一個評論。 – Zippy

+0

我沒有足夠的聲望發表評論,讓它成爲) – schmidt9

相關問題