开发者

Please help: UINavigationController and view controllers memory management

I have a strange problem with a method is called each time a button is pressed:

- (void)launcherView:(TTLauncherView*)lnchr didSelectItem:(TTLauncherItem*)itm {
  MyObject* obj = ...

  MyViewController* detailView = [[MyViewController alloc] init];  // line A
  [self.navigationController pushViewController:detailView animated:YES];
  [detailView setObject:obj];
  detailView = nil;  // should I also release it? -- line B
}

The problem is that I apologize I have to release detailView (memory tool shows me I have a memory le开发者_运维问答ak is it is not done), also because navigationController should retain my detailView, but both if I try to add autorelease in line "A" or in line "B", or simply a release for detailView in line "B" (of course before assigning it nil), the program crashes with an EXC_BAD_ACCESS 'cause release message sent to deallocated instance [CALayer]...

Any idea? Thanks a lot


try it this way

- (void)launcherView:(TTLauncherView*)lnchr didSelectItem:(TTLauncherItem*)itm {
  MyObject* obj = ...

  MyViewController* detailView = [[MyViewController alloc] init]; 
  [detailView setObject:obj];
  [self.navigationController pushViewController:detailView animated:YES];
  [detailView release];
  detailView = nil;  // now this will be optional
}


Does this work without crashing?

- (void)launcherView:(TTLauncherView*)lnchr didSelectItem:(TTLauncherItem*)itm {
  MyObject* obj = ...

  MyViewController* detailView = [[MyViewController alloc] init];
  [self.navigationController pushViewController:detailView animated:YES];
  //[detailView setObject:obj];  // <- What's this for?
  [detailView release]
}


try "initwithnibname"

unrelated but if you chase memory leaks don't forget to release MyObject


When you set detailView = nil; without releasing it, you only nil the pointer to the memory. The block of memory is still allocated until you release it.

You must use [detailView release] before detailView = nil or else you will have no way to reference that block of memory again (memory leak).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜