変数の3分の1だけを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?
}
他の答えは、ダングリングポインター(たとえば、ビューが後ろで変わった可能性がありますが、ビューへのポインターを保持している)が許容されることを前提としています。
実際のプログラムでは許可されるべきではありません。それらは非常に危険であり、それらが引き起こすエラーを再現することは非常に困難です。したがって、維持/保持するポインターへの参照を所有していることを確認する必要があります。
また、サブクラッサーのために、パブリックインターフェイスでアクセサーを使用する必要があります-それらがオーバーライドする場合に備えて。それを許可/サポートしたくない場合は、単にプライベート変数を使用することを検討してください。