【发布时间】:2010-12-19 16:43:11
【问题描述】:
我正在整理我的 iPhone 应用程序的一些内存问题,并且我一直在考虑一些基础知识。如果我设置了一个 ivar 并且在对象的生命周期内从未使用过它,那么当我在其上调用 dealloc 时,这会导致问题吗?例如
@interface testClass {
id myobject;
}
@property (nonatomic, retain) id myobject;
@end
@implementation testClass
@synthesize myobject;
- (id)init {
...
// Do I have to set myobject to nil here?
// So if myobject isn't used the dealloc call to nil
// will be okay? Or can you release the variable without
// having set every object to nil that you may may not use
...
}
...
// Somewhere in the code, myobject may be set to
// an instance of an object via self.myobject = [AnObject grabAnObject]
// but the object may be left alone
...
- (void)dealloc {
[myobject release];
[super dealloc];
}
@end
【问题讨论】:
-
Mike Abdullah:我在编辑中做出了改变。
-
啊,对了,那么在函数中创建的普通变量在声明它们时没有设置为 0/nil 吗?只是实例变量。那么,在您明确将其设置为某个值之前,普通变量只包含“垃圾”是否正确?
-
@MichaelWaterfall Local object variables are automatically initialized to nil.
-
...当您使用 ARC 时,您应该这样做。
标签: iphone objective-c cocoa cocoa-touch memory-management