2012-08-30 45 views
5

Tôi biết lỗi này có liên quan đến quản lý bộ nhớ nhưng tôi phải thừa nhận rằng tôi đã bị lỗi! Được lập trình trong mục tiêu c trong khoảng 3 tuần và tất cả các công cụ bộ nhớ quản lý này là khó hiểu! Điều cơ bản xảy ra là tôi có mapview này trong một tableview. Khi nhấp vào nút quay lại để thoát khỏi chế độ xem bản đồ và quay lại menu chính, tôi nhận được lỗi ở trên. Đây là mã từ tập tin headerEXC_BAD_ACCESS (code = 1) Lỗi trong 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 

và tập tin thi hành

#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 

Ai có thể làm sáng tỏ cho tôi? :)

+0

Tôi đã thử cho ý kiến ra dòng khác nhau của mã để xem nếu tôi có thể làm việc nó ra nhưng nó là cùng một điều mọi thời gian, như tôi đã nói tôi khá mới này để giúp đỡ bất kỳ, ngay cả với gỡ lỗi, sẽ được đánh giá cao :) (Xin lỗi là một clueless đau ở mặt sau lol) – Craig

Trả lời

12

[super dealloc]; phải là cuộc gọi cuối cùng trong dealloc

cũng sau [mapView release]; MapView có thể đi rồi.

thử

- (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

Vẫn gặp sự cố với lỗi tương tự mà tôi sợ: ( – Craig

+1

bạn có thể cung cấp thông báo lỗi đầy đủ không? – vikingosegundo

+0

EXC_BAD_AC CESS (mã = 1, địa chỉ = 0xa0e69c14) nhưng địa chỉ bộ nhớ thay đổi mỗi lần. Chỉ cần chỉnh sửa để thêm một số sự rõ ràng như tôi quên đề cập đến khi ứng dụng bị lỗi>. < – Craig

0

Lỗi này cũng có thể được gây ra bởi API tương thích (ví dụ. Bạn xây dựng cho iOS 6.0, nhưng sử dụng một phương pháp được giới thiệu chỉ trong iOS> = 8.2)

+1

Đây không phải là một câu trả lời, giống như một bình luận. – Zippy

+0

Tôi không đủ danh tiếng để đăng nhận xét, hãy để ý kiến ​​đó) – schmidt9

Các vấn đề liên quan