1

我还有一个假设性问题。

我创建功能:

-(IBAction)text:(id)sender{
    UITextView * textview = [[UITextView alloc]initWithFrame:CGRectZero];
}

并且因为我使用 ARC 我希望当函数超出范围时该对象会被释放。但是使用 Instrument 我检测到内存没有被释放。

有谁知道为什么?

4

2 回答 2

0

无论是否为 nil,ARC 都会自动释放/释放 textView


将其全部包装在 @autoreleasepool 中即可!

-(IBAction)text:(id)sender{
   @autoreleasepool {
        UITextView * textview = [[UITextView alloc]initWithFrame:CGRectZero];
    }
}

As for the initial grow when the textview is used the first time: the cocoa touch text system is allocated. (The underlying 'engine' that all textviews share)

于 2013-02-01T08:51:54.053 回答
-5

如果你想真正释放它,试试这个。

-(IBAction)text:(id)sender{
    UITextView * textview = [[UITextView alloc]initWithFrame:CGRectZero];
    textview = nil;
}

ARC 只是自动添加发布代码,它不像其他语言那样具有垃圾收集器。只要你给一个对象 nil ,它就会立即被释放。否则,当添加此对象的“自动释放池”被刷新时,它会被释放。(虽然我不确定最后一点。)。

于 2013-02-01T08:33:39.457 回答