AndrewO
AndrewO

Reputation: 1639

Memory leak in loadNibNamed?

I'm about to wrap up my first iPhone app and figured I'd run it through the Leaks Performance Tool. After fixing one obvious one, the only one I have left is one with a Nib acting as a table header view loaded through loadNibNamed (I was following the Recipes demo here).


- (void)viewDidLoad {
    [super viewDidLoad];

    if (self.tableHeaderView == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"TableHeaderView" owner:self options:nil];
        self.tableView.tableHeaderView = self.tableHeaderView;
    }
}

Then in dealloc:


- (void)dealloc {
    [tableHeaderView release];
    [super dealloc];
}

Instruments tells me that I'm leaking 256 bytes with 2 leaks coming from the line with loadNibNamed. tableHeaderView is the only top level object in the Nib (I've verified that in the debugger). Is there something I'm forgetting to release? Am I misinterpreting what Instruments is telling me? Is it wrong? Is it something that the OS will clean up later?

Upvotes: 6

Views: 4166

Answers (2)

NSResponder
NSResponder

Reputation: 16861

When you load a nib, you're responsible for releasing all of the top-level objects in the nib file. Is there anything in that file besides the TableHeaderView?

Upvotes: 3

inked
inked

Reputation: 624

Is Instruments telling you this solely on the Simulator, or is it reporting the same thing on an actual device? If you don't get it on the device, then it's the Simulator - and that's known to happen (it isn't an exact match).

Also, down in dealloc, wouldn't it be [self.tableHeaderView release]? You have to be consistent with your usage.

To avoid confusion, in your .h, you'd declare this:

NS/UI/??xxxxxx *_MyObjectName;   //notice the underscore

Then the Property like this:

@property .... NS/UI/??xxxxxx *MyObjectName;   //no underscore

Then synthesize the getters/setters like this:

@synthesize MyObjectName=_MyObjectName;

Finally, refer to the object throughout the program with [self.MyObjectName ...];

Upvotes: 2

Related Questions