使用loadNibNamed会导致内存泄漏 [英] Using loadNibNamed leaves a memory leak

查看:401
本文介绍了使用loadNibNamed会导致内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,使用loadNibNamed:会让我忘记内存。

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

假设我有接口:

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

步骤:

@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];
}

我使用以下方式执行init:

I perform the init using:

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

我似乎无法弄清楚为什么永远不会调用dealloc方法。
在Xib中,文件的所有者是Step,而keyPadPopupView在IB中连接。

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.

我有什么遗漏吗?

谢谢!

推荐答案

在iOS中连接IBOutlet会导致保留对象(与OS X)。将视图添加到子视图会导致保留该视图。所以......

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...

从笔尖加载 - +1(1)

Load from nib - +1 (1)

添加为子视图 - +1( 2)

Add as subview - +1 (2)

发布 - -1(1)

release - -1 (1)

您仍然有未完成的保留。

You still have an outstanding retain.

是否正在调用viewDidUnload?通常在那里你释放所有保留的子视图。

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

这篇关于使用loadNibNamed会导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆