2011-05-02 73 views
2

我在UIViewController中有一個MapView。當我嘗試它viewDidUnload後加載視圖其與此消息崩潰:「[CALayer發佈]:發送到釋放實例的消息」在UIViewController中

- [CALayer的發行]:消息發送到釋放實例0x29aea0

,我認爲我做的一切必要的事情,我應該做我的viewDidUnload:

- (void)viewDidUnload { 

    [super viewDidUnload]; 

    locationManager.delegate = nil; 
    [locationManager release]; 
    locationManager = nil; 

    mapView.delegate = nil; 
    [mapView release]; 
    mapView = nil; 
} 

我的MapView是在一個配置在xib文件中的UIView中。我的VC永遠不會被釋放。

我一直在搜索一下,但我找不到答案。

編輯

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    locationManager = [[CLLocationManager alloc] init]; 
    [locationManager setDelegate:self]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
    [locationManager startUpdatingLocation]; 

    [label setFont:[UIFont fontWithName:@"Sansation" size:28]]; 
    [label setShadowOffset:CGSizeMake(0, 1)]; 
    [label setShadowColor:[UIColor grayColor]]; 
    self.navigationItem.titleView = labelView; 
    [label release]; 
    [labelView release]; 

    UIBarButtonItem *checkInButton = [[UIBarButtonItem alloc] initWithTitle:@"Checka In" style:UIBarButtonItemStylePlain target:self action:@selector(checkIn)]; 
    [[self navigationItem] setRightBarButtonItem:checkInButton]; 
    [checkInButton release]; 

    UIBarButtonItem *clueListButton = [[UIBarButtonItem alloc] initWithTitle:@"Ledtrådar" style:UIBarButtonItemStylePlain target:self action:@selector(cluesDown)]; 
    [[self navigationItem] setLeftBarButtonItem:clueListButton]; 
    [clueListButton release]; 

    UINavigationBar *bar = [self.navigationController navigationBar]; 
    [bar setTintColor:[UIColor colorWithRed:0.06 green:0.58 blue:0.88 alpha:1]]; 
} 

「LABELVIEW」 和 「標籤」 是IBOutlets。

+0

locationManager和mapView實際上是否真的保留了嗎?我想他們只是添加爲子視圖而沒有保留,然後'[super viewDidUnload];'可能會釋放它們,釋放會導致崩潰。 – DarkDust 2011-05-02 10:13:11

+0

locationManager = [[CLLocationManager alloc] init];和@property(nonatomic,retain)IBOutlet MKMapView * mapView ;.這是正確的方式嗎?如果[super viewDidUnload]釋放對象,我應該在viewDidUnload中獲得exc_bad_access?該錯誤出現在viewDidAppear之後,但是在你實際看到mapView(就是網格)之前。 – 2011-05-02 10:20:52

+0

訪問釋放對象時,您不一定會遇到訪問異常錯誤。如果新對象現在駐留在該內存位置,您也可以將*消息發送到釋放實例*錯誤或*無法識別的選擇器*。但是你現在說這個消息來自* viewDidAppear *之後,所以你應該向我們展示這個方法。 viewDidUnload方法在視圖控制器釋放其視圖(清除)時調用。 – DarkDust 2011-05-02 10:51:41

回答

7

由於您既沒有在viewDidLoad方法中分配也沒有保留labellabelView,因此您可能無法在此處發佈它們(您正在重新發布它們)。

+0

非常感謝!這是問題!我很抱歉我的無知 - 仍在學習:) – 2011-05-02 11:25:50

+0

@Erik Nolander:不客氣。在學習Objective-C時,內存管理是最大的問題,但是一旦你瞭解並理解了規則,它實際上很簡單。 – DarkDust 2011-05-02 11:32:30

0

因爲你做了一些不需要的發佈,比如類方法的返回值。你可以釋放你所有的再訓練對象 - (void)dealloc

相關問題