你如何在 xcode 4.2 中释放内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7734502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you release memory in xcode 4.2?
提问by iphonemaniac
In xcode 4.2 I have found it very frustrating because you can't use:
在 xcode 4.2 中,我发现它非常令人沮丧,因为你不能使用:
-(void)dealloc {
[label release]; //'release' is unavailable
[super dealloc]; //'dealloc' is forbidden in automatic reference counting
}
Is there another way because autorelease and other deallocs don't work either.
有没有另一种方法,因为 autorelease 和其他 deallocs 也不起作用。
回答by BJ Homer
Xcode 4.2 introduces "Automatic Reference Counting" (aka ARC). This is a compiler feature that basically inserts the retain and release calls for you. Under ARC, if you have a pointer to an object, you're retaining it. When your pointer goes out of scope, or is reassigned to point to another object, the original object is released. It's really nice.
Xcode 4.2 引入了“自动引用计数”(又名 ARC)。这是一个编译器功能,基本上为您插入了保留和释放调用。在 ARC 下,如果你有一个指向对象的指针,你就是在保留它。当您的指针超出范围或被重新分配以指向另一个对象时,原始对象将被释放。这太好了。
So, in short, you just remove all the calls to retain
, release
, and autorelease
, and the compiler will do the right thing foryou.
因此,简而言之,您只需删除对、 和 的所有调用retain
,编译器就会为您做正确的事情。release
autorelease
回答by Flyingdiver
Read up on Automatic Reference Counting. If you write your code properly, you don't need to do any of that anymore.
阅读自动引用计数。如果您正确编写代码,则不再需要执行任何操作。
If you want to use old code without converting, disable ARC. put -fno-objc-arc
in the compiler flags for any source modules you don't want to use ARC.
如果您想在不转换的情况下使用旧代码,请禁用 ARC。-fno-objc-arc
为您不想使用 ARC 的任何源模块放入编译器标志。
joe
乔