5

このエラーがメモリ管理に関係していることは知っていますが、困惑していることを認めなければなりません。約3週間ObjectiveCでプログラミングしていて、このすべてのメモリ管理は混乱を招きます。基本的に起こっていることは、私がこのマップビューをテーブルビューに持っているということです。戻るボタンをクリックしてマップビューを終了し、メインメニューに戻ると、上記のエラーが発生します。これがヘッダーファイルのコードです

#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

誰かが私のためにいくつかの光を当てることができますか?:)

4

2 に答える 2

12

[super dealloc];最後の呼び出しである必要がありますdealloc

また、[mapView release];mapViewがすでになくなっている可能性があります。

試す

- (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
}
于 2012-08-30T13:22:49.033 に答える
0

このエラーは、互換性のないAPIによっても発生する可能性があります(たとえば、iOS 6.0用にビルドしますが、iOS> = 8.2でのみ導入されたメソッドを使用します)

于 2016-02-02T22:00:16.193 に答える