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

変数の3分の1だけをdeallocメソッドで解放する必要があると思います。そうですか?

4

5 に答える 5

0

変数の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?
}

他の答えは、ダングリングポインター(たとえば、ビューが後ろで変わった可能性がありますが、ビューへのポインターを保持している)が許容されることを前提としています。

実際のプログラムでは許可されるべきではありません。それらは非常に危険であり、それらが引き起こすエラーを再現することは非常に困難です。したがって、維持/保持するポインターへの参照を所有していることを確認する必要があります。

また、サブクラッサーのために、パブリックインターフェイスでアクセサーを使用する必要があります-それらがオーバーライドする場合に備えて。それを許可/サポートしたくない場合は、単にプライベート変数を使用することを検討してください。

于 2011-03-17T08:55:25.287 に答える
0

はい、(arrayリリースする必要があります)あなたがallocそれだからです。したがって、それをリリースするのはプログラマーの責任です。それで -

- (void)dealloc {

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

リリースするものの詳細については、メモリ管理-ObjectiveC

于 2011-03-17T06:34:24.107 に答える
0

そして、あなたのクエリに関するこの質問で良い提案が見つかると思います

なぜリリースする必要があるのですか?

于 2011-03-17T06:36:31.670 に答える
0

一部の回答に反して、アウトレット (ビュー) も解放する必要があります。dealloc だけでなく、viewDidUnload でも、最も簡単な方法は nil に設定することです。

self.view = nil;

また、プロパティにアクセスせずにインスタンス変数 (つまり、self.プレフィックスなし) にアクセスする場合、retain 属性は役に立たず、オブジェクトを保持していないことに注意してください。つまりscopeBar、 のサブビューから削除されるとすぐviewに解放され、ゾンビにアクセスすることになります。

経験則として、メモリ管理を明示的に処理する必要がないように、init メソッドを除くすべての場所でプロパティ アクセサーを使用することをお勧めします。アウトレットの場合は、dealloc と viewDidUnload でそれらを nil に設定するだけで十分です。

また、ジェニファーが提案したことを行わないでください。変数のリリースを呼び出したら、プロパティを nil に設定しないでください。

于 2011-03-17T08:11:26.083 に答える
-2

プロパティを作成したため、解放してnilに設定する必要があると思うので、これを行います:-

あなたのdeallocで

[array release];
self.array=nil;
self.scopeBar=nil;
self.view=nil;
于 2011-03-17T08:01:56.073 に答える