Objective C memory leak problem

The Leaks tool tells me that I have a leak in this piece of code. Why is this so?

This piece of code is in viewDidLoad()

.

UINavigationItem *navItem=[self navigationItem];

UIBarButtonItem *addFeed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addFeed)];
[navItem setRightBarButtonItem:addFeed]; // leaks says that 128 bytes leaked
[addFeed release];

UIBarButtonItem *reload = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh  target:self action:@selector(reload)];
[navItem setLeftBarButtonItem:reload]; // leaks says that 128 bytes leaked here too !
[reload release];
[navItem release];

      

+2


source to share


6 answers


You mustn't let go navItem

. You haven't selected / saved / created / created it, so you won't let it go.



Other than that, your code looks great. Is it all in the method?

+6


source


The leak tool only indicates where the memory leak has been allocated; it cannot tell you where the memory should have been released, but it hasn't, since there is no possible way to find out. Your leak is happening elsewhere.



This code is mostly fine, except that you shouldn't let go navItem

at the end. You do not own it, because you did not create it with a method named alloc

, new

or copy

in its name, so it should not be released.

+3


source


if you still get a leak and can't track down the error, you can try using the static analyzer included in the latest and greatest Xcode (version 3.2)

Assembly> Assembly and Analysis

it will use LLVM-Clang to statically analyze your code in a nice way.

http://developer.apple.com/mac/library/featuredarticles/StaticAnalysis/index.html

UPDATE:

in the code snippet:

UINavigationItem *navItem=[self navigationItem];

UIBarButtonItem *addFeed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addFeed)];
[navItem setRightBarButtonItem:addFeed]; // leaks says that 128 bytes leaked
[addFeed release];

      

your leak probably comes from setting a new rightBarButtonItem without dropping the old one.

This is what I think is happening:

1) get a handle to the navigationItem (has a right bar button A)

2) create a new UIBarButton element (make the right B button)

3) setRightBarButtonItem to button B

now that button A? it should be released by navItem when you install the new button. so you might forget to release the button when you install it the first time, or you might have saved it somewhere else.

+2


source


Do you have NSZombieEnabled? This causes objects to not be persisted by NSZombie instances, and you will see "leaks" when you run the Leaks tool.

0


source


It seems that you are not releasing the view controller with a custom method viewDidLoad

.

0


source


 [navItem setRightBarButtonItem:addFeed];

 [navItem setLeftBarButtonItem:reload];

      

You create copies of the objects in these accessories. These accessors increment the keepCount value by 1. Your accessors must release each object and then immediately save them.

Example:

- (void) setTitle: (NSString*) newTitle {
    if (title != newTitle) {
        [title release];
        title = [newTitle retain]; // Or copy, depending on your needs.
    }

      

Take a look at the techniques here: Memory Management Programming

I believe that. So take a close look at these two accessors.

0


source







All Articles