0

I've created UITabBarController programmatically, like this

mTabBarController = [[UITabBarController alloc] init];
...
NSLog(@"The ref count is : %d", [tabBarItems retainCount]);
mTabBarController.viewControllers = tabBarItems;
NSLog(@"The ref count is : %d", [tabBarItems retainCount]);
[tabBarItems release];
NSLog(@"The ref count is : %d", [tabBarItems retainCount]);

Also releasing mTabBarController in dealloc, like this,

- (void)dealloc {
    [mTabBarController release];
    ...
}

Now the question : The output for the first code snippet is

2011-11-01 17:48:26.554 PostCardPrinter[12176:207] The ref count is : 1
2011-11-01 17:48:26.561 PostCardPrinter[12176:207] The ref count is : 1
2011-11-01 17:48:26.561 PostCardPrinter[12176:207] The ref count is : 1

Am I getting memory leak ? And why it prints 1 always ?

if it retains tabBarItems then the second output should be 2. If

mTabBarController.viewControllers = tabBarItems;

copies array items and retains each each array item then, the 3rd output should b 2 right ?

Do I get something wrong ???

deimus
  • 9,565
  • 12
  • 63
  • 107
  • 3
    Let the retainCount rant begin! -Edit: found it http://stackoverflow.com/questions/4636146/when-to-use-retaincount – Joe Nov 01 '11 at 14:03
  • OK, since I'm new t iOS dev, I really need that your comment ;) btw you better write an answer than comment ... – deimus Nov 01 '11 at 14:07
  • @Joe I was looking forward to more of a rant than that. I was the only participant! – James Webster Nov 01 '11 at 14:22
  • @JamesWebster I am thinking most people are just over it by now :) Apple really should not have exposed it as a public API at least with ARC it is now illegal to call it. – Joe Nov 01 '11 at 14:33

1 Answers1

2

Retain count bad: When to use -retainCount?

In short: You can't guarantee retainCount will return a sensible value.

Community
  • 1
  • 1
James Webster
  • 31,873
  • 11
  • 70
  • 114