UIView *view; //1
UISegmentedControl *scopeBar; //2
NSMutableArray *array; //3
@property (nonatomic, retain) IBOutlet UIView *view;
@property (nonatomic, retain) UISegmentedControl *scopeBar;
@property (nonatomic, retain) NSMutableArray *array;
.m
@synthesize view, scopeBar, array;
for (id subView in [view subviews]) {
if ([subView isMemberOfClass:[UISegmentedControl class]]) {
scopeBar = (UISegmentedControl *)subView;
}
}
array = [[NSMutableArray alloc] init];
- (void)dealloc {
}
我认为只需要在dealloc方法中释放第三个变量。 是吗?
答案 0 :(得分:0)
是的,(array
需要发布),因为你alloc
。因此,程序员有责任发布它。所以 -
- (void)dealloc {
[ array release ] ;
// Any other resources alloc, init, new should be released
}
有关发布的内容的详情,Memory management - ObjectiveC
答案 1 :(得分:0)
我认为你会在这个问题中找到关于你的查询的好建议
答案 2 :(得分:0)
与一些答案相反,你必须释放你的插座(视图),不仅在dealloc中,而且在viewDidUnload中,最简单的方法是将它设置为nil:
self.view = nil;
另请注意,如果您不访问属性但实例变量(即没有self.
前缀),则您的retain属性将无法帮助您,并且您不会保留该对象。这意味着只要scopeBar
从view
的子视图中删除,它就会被释放,您最终会访问僵尸。
根据经验,除了init方法之外,最好在所有地方使用属性访问器,这样您就不必明确地处理内存管理。在dealloc中将它们设置为nil,在出口的情况下将viewDidUnload设置为nil。
另外,不要做Jenifer建议的内容,一旦你在一个变量上调用了一个版本,就不要将该属性设置为nil,这会过度发布它。
答案 3 :(得分:0)
我认为只需要在dealloc方法中释放第三个变量。是吗?
// no. your dealloc should look like this:
- (void)dealloc {
// note: *not* using accessors in dealloc
[view release], view = nil;
[scopeBar release], scopeBar = nil;
[array release], array = nil;
[super dealloc];
}
// your assignment of `scopeBar` should look like this:
...
self.scopeBar = (UISegmentedControl *)subView;
...
// you want to retain the view, as advertised.
// consider avoiding an ivar if you can easily access it.
// your assignment of `view` should look like this:
...
self.view = theView;
...
// you want to retain the view, as advertised.
// consider avoiding an ivar if you can easily access it.
// your assignment of `array` should look like this in your initializer:
// note: *not* using accessors in initializer
...
// identical to `array = [[NSMutableArray alloc] init];`
array = [NSMutableArray new];
...
// and the assignment of `array` should look like this in other areas:
...
self.array = [NSMutableArray array];
...
// you're likely to be best suited to declare your array as
// follows (assuming you really need a mutable array):
...
NSMutableArray *array; // << the declaration of the ivar
...
...
// the declaration of the public accessors.
// note the array is copied, and passed/returned as NSArray
@property (nonatomic, copy) NSArray *array;
...
// finally, the implementation manual of the properties:
- (NSArray *)array {
// copy+autorelease is optional, but a good safety measure
return [[array copy] autorelease];
}
- (void)setArray:(NSArray *)arg {
NSMutableArray * cp = [arg mutableCopy];
// lock? notify?
NSMutableArray * prev = array;
array = cp;
[prev release], prev = nil;
// unlock? notify? update?
}
其他答案假设悬空指针(例如,您仍然持有指向视图的指针,尽管视图可能已在您的背后发生变化)是允许的。
他们不应该被允许进入真正的节目。它们非常危险,很难再现它们造成的错误。因此,您必须确保拥有对您维护/保持的指针的引用。
为了子类的原因,你也应该使用公共接口中的访问器 - 以防它们覆盖它们。如果您不想允许/支持,请考虑使用私有变量。
答案 4 :(得分:-2)
我认为你应该释放并设置它们为零因为你已经制作了它们属性所以这样做: -
你的dealloc 中的
[array release];
self.array=nil;
self.scopeBar=nil;
self.view=nil;