mmilo
mmilo

Reputation: 806

Using loadNibNamed leaves a memory leak

For some reason, using loadNibNamed: is leaving me with a memory leak.

Let's say I have the interfaces:

@interface Step : UIViewController
{
  IBOutlet UIView *keyPadPopupView;
}
@property (nonatomic, assign) IBOutlet UIView *keyPadPopupView;

In Step:

@synthesize keyPadPopupView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
  {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
    {
        [[NSBundle mainBundle] loadNibNamed:@"customNumberKeypad" owner:self options:nil];
        [self.view addSubview:keyPadPopupView];
        [keyPadPopupView release];
    }
    return self;
  }

- (void) dealloc
{
    NSLog(@"dealloc........%@", [self class]);
    [super dealloc];
}

I perform the init using:

Step *step = [[Step alloc] initWithNibName:@"StepXib" bundle:nil];
[step release]; 

I can't seem to figure out why the dealloc method is never called. Inside the Xib, the file's owner is Step, and the keyPadPopupView is connected in IB.

Is there something I'm missing?

Thanks!

Upvotes: 2

Views: 3544

Answers (1)

vagrant
vagrant

Reputation: 978

In iOS connecting an IBOutlet causes the object to be retained (unlike OS X). Adding a view to a subview causes it to be retained. So...

Load from nib - +1 (1)

Add as subview - +1 (2)

release - -1 (1)

You still have an outstanding retain.

Is viewDidUnload being called? Normally in there you release all the retained subviews.

Upvotes: 2

Related Questions