Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this code producing an memory leak?

Tags:

The Leaks Instrument in Xcode shows me an memory leak here. I have commented the affected line which Leaks is complaining about. But I see no error in my memory management...

- (void)setupViewController {
    MyViewController *myVC = [[MyViewController alloc] init];

    UITabBarItem *tbi = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:1];
    myVC.tabBarItem = tbi; // LEAK: 128 bytes

    self.myViewController = myVC;

    [myVC release];
    [tbi release];
}

I mean... tbi and myVC IS released at the end, and the alloc IS balanced. So what's wrong? I don't get it.

like image 350
dontWatchMyProfile Avatar asked Feb 15 '10 20:02

dontWatchMyProfile


2 Answers

if MyVc.tabBarItem is already set, whatever it's pointing at may not be deallocated properly, causing a leak.

like image 61
patros Avatar answered Oct 11 '22 23:10

patros


It just goes to show that at least one of the following statements is true:

  1. Instruments is not perfect and sometimes shows leaks where there aren't any (and vice versa).
  2. Apple's code is not bug-free.

In fact, both are true.

like image 39
Ole Begemann Avatar answered Oct 12 '22 00:10

Ole Begemann