Dmitri Sologoubenko
Dmitri Sologoubenko

Reputation: 2909

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

Upvotes: 0

Views: 528

Answers (4)

Christopher Moore
Christopher Moore

Reputation: 149

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).

Upvotes: -1

Bogdan S
Bogdan S

Reputation: 1

try "initwithnibname"

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

Upvotes: 0

Kirby Todd
Kirby Todd

Reputation: 11556

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]
}

Upvotes: 1

Vaibhav Tekam
Vaibhav Tekam

Reputation: 2344

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
}

Upvotes: 2

Related Questions