0

I've the following question:

I've an AppDelegate and I add there a navigationController and load UIViewController B in it.

In B I add a navigationItem, a leftBarButtonItem or a rightBarButtonItem.

Where do I have to release these items, because I alloc and init them in B. So at the first I thought about releasing self.navigationItem.rightBarButtonItem in Dealloc-Method of B.

But if I'm analyzing my app, the analyzer says at the release-position in B's dealloc-method:

Incorrect decrement of the reference count of an object that is not owned at this point by the caller.

But I don't understad what I've done wrong or is everything ok and it's an analyzer problem?

Can somebody help me finding out?

Greets andi1984

andi1984
  • 676
  • 10
  • 27
  • Do you autorelease the navigation item(s) by any chance while creating them? Can you show the code where you create these navigatin items? – Chaitanya Gupta Aug 31 '11 at 20:16

1 Answers1

1

As soon as you allocate and assign to rightBarButtonItem you can release like

UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showNewEventViewController)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
[rightBarButtonItem release];

Similarly to leftBartButtonItem

self.navigationItem.rightBarButtonItem might have a retain in itself and knows when to release that count.

Saran
  • 6,274
  • 3
  • 39
  • 48
  • So I don't need to release self.navigationItem.rightBarButtonItem itself manually? – andi1984 Aug 31 '11 at 20:30
  • No. But make sure you do [rightBarButtonItem release] after you assign it to self.navigationItem.rightBarButtonItem. – Saran Aug 31 '11 at 20:37