【问题标题】:"[CALayer release]: message sent to deallocated instance" in UIViewControllerUIViewController 中的“[CALayer 发布]:发送到已释放实例的消息”
【发布时间】:2011-08-16 21:16:41
【问题描述】:

我在 UIViewController 中有一个 MapView。当我尝试在 viewDidUnload 之后加载视图时,它会崩溃并显示以下消息:

-[CALayer release]:消息发送到deallocated instance 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”和“label”是 IBOutlets。

【问题讨论】:

  • locationManager和mapView真的真的保留了吗?我想它们只是作为子视图添加而没有保留,然后[super viewDidUnload]; 可能会释放它们,而您的释放会导致崩溃。
  • locationManager = [[CLLocationManager alloc] init];和@property(非原子,保留)IBOutlet MKMapView *mapView;。这是正确的方法吗?如果 [super viewDidUnload] 正在释放对象,我应该在 viewDidUnload 中获得 exc_bad_access 吗?错误出现在 viewDidAppear 之后,但在您真正看到 mapView(只是网格)之前。
  • 在访问已释放的对象时不一定会遇到错误的访问异常。如果新对象现在位于该内存位置,您还可以获得 message sent to deallocated instance 错误或 unrecognized selector。但是你现在说消息是在 viewDidAppear 之后出现的,所以你应该向我们展示这个方法。 viewDidUnload 方法在视图控制器释放其视图(清理)时被调用。
  • viewDidAppear 方法中没有写任何内容。我只是在那里有一个断点,看看它是否被调用。我将编辑我的问题并添加 viewDidLoad 方法。

标签: iphone objective-c release android-mapview calayer


【解决方案1】:

由于您在viewDidLoad 方法中既没有分配也没有保留labellabelView,所以您不能在此处释放它们(您过度释放它们)。

【讨论】:

  • 非常感谢!这就是问题所在!我很抱歉我的noobyness - 还在学习:)
  • @Erik Nolander:不客气。内存管理是学习 Objective-C 时最大的问题,但是一旦你学习并理解了规则,它实际上非常简单。
【解决方案2】:

因为你做了一些不需要的发布,比如类方法返回值。你可以在 -(void)dealloc

中释放所有你重新训练的对象

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 2011-07-15
    • 2011-06-16
    相关资源
    最近更新 更多