2011-03-17 55 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? 
} 

其他答案假設懸擺指針(例如,你仍然持有一個指針來查看,但認爲可能已經在你的背後改變)是允許的。

他們不應該被允許在真正的程序。他們是非常危險的,並且很難重現他們造成的錯誤。因此,您必須確保您擁有對您維護/保持的指針的引用。

你也應該在公共接口中使用訪問器爲了子類型的緣故 - 如果它們覆蓋它們。如果你不想允許/支持,只需考慮使用一個私有變量。