内存不会减少释放对象时

时间:2012-04-14 05:15:26

标签: iphone objective-c ios ipad

(^。^)“嗨再次抱歉我的英语不好,如果有人喜欢纠正我的修改,我会很感激”

是的,你是对的。但: 首先,当我单击创建按钮时,这将创建带有alloc的新视图控制器,并自动保留计数+1,当我按下kill按钮时,保留计数-1和等于0这意味着视图控制器创建理论上它已被删除表单内存我更正了代码:

- (IBAction)create:(id)sender{
    if(vc == nil){ //if is not nil this mean vc have some space of memory reference and vc is not created
    //if == nil this mean vc does not have space of memory reference so create.
    vc = [[VC alloc] initWithNibName:@"VC" bundle:[NSBundle mainBundle]];// retain count + 1
    [_VW addSubview:vc.view];
}

  - (IBAction)kill:(id)sender{
    [vc.view removeFromSuperview]; //When view removeFromSuperview is called also dealloc is called of the vc view
    [vc release];// retain count - 1  the curren count is equal 0 this mean vc does not have space of memory
     vc = nil; // remove the reference of memory.
  }

* 但是当我制作proyect的个人资料并且我点击按钮创建并杀死内存时不减少只增长*

抱歉但是我不能粘贴图像,因为我是Newbie的帖子,但是当我在Allocations init中使用Live Bytes 584,19kb启动配置文件时,在1分钟内实时字节数为1,08 mb,不释放任何东西只会增长。< / p>

我现在不知道如果我创建并正确免费请帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用以下两种方式 - 1.分配一次并以dealloc发布 -

- (IBAction)create:(id)sender{
    if(vc == nil){ 
        vc = [[VC alloc] initWithNibName:@"VC" bundle:[NSBundle mainBundle]];
        [_VW addSubview:vc.view];
    }
}

- (IBAction)kill:(id)sender{
    [vc.view removeFromSuperview]; 
}

- (void)dealloc {
    [vc release];

    [super dealloc];
}

2。每次分配并释放 -

- (IBAction)create:(id)sender{
    if(vc == nil){ 
        vc = [[[VC alloc] initWithNibName:@"VC" bundle:[NSBundle mainBundle]] autorealease];
        [_VW addSubview:vc.view];
    }
}

- (IBAction)kill:(id)sender{
    [vc.view removeFromSuperview]; 
}

现在您可以尝试其中任何一项,然后检查内存占用量。