2011-03-17 56 views
1
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

是的,(array需要发布)因为你alloc它。所以,程序员有责任释放它。所以 -

- (void)dealloc { 

    [ array release ] ; 
    // Any other resources alloc, init, new should be released 
} 

有关如何发布更多信息,Memory management - ObjectiveC

-2

,因为我认为你应该释放并设置它们为零,因为你已经使它们属性如此做: -

在你的dealloc

[array release]; 
self.array=nil; 
self.scopeBar=nil; 
self.view=nil; 
+0

要么释放(并可选择将伊娃设置为零),要么调用setter--永远都不会! – 2011-03-17 14:19:22

0

相反,一些问题的答案,你必须释放你的插座(视图)为好,而不是只在的dealloc而且在viewDidUnload,最简单的方法是将其设置为nil:

self.view = nil;

还要注意的是,如果你不访问您的属性,但您的实例变量(即没有self.前缀)您的保留属性不会帮助您,并且您不保留该对象。这意味着只要scopeBar将从view的子视图中删除,它就会被释放,并最终访问僵尸。

作为一个经验法则,除init方法外,最好使用属性访问器,这样就不必显式地处理内存管理。在dealloc中设置它们为零,并且在outlet的情况下将它们设置为viewDidUnload应该足够了。

另外,不要做什么Jenifer建议,并且一旦你调用了一个变量释放,不要设置该属性为零,这会超额释放它。

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? 
} 

其他答案假设悬摆指针(例如,你仍然持有一个指针来查看,但认为可能已经在你的背后改变)是允许的。

他们不应该被允许在真正的程序。他们是非常危险的,并且很难重现他们造成的错误。因此,您必须确保您拥有对您维护/保持的指针的引用。

你也应该在公共接口中使用访问器为了子类型的缘故 - 如果它们覆盖它们。如果你不想允许/支持,只需考虑使用一个私有变量。