0

アプリのメソッドに次のコードがあります。

NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];

[someObject someMethodThatTakesAnArray:array];

arrayWithObjects メソッドが自動解放された配列を返すことは知っています。これは、作成後すぐに保管する必要があるということですか? 配列を someMethodThatTakesAnArray に渡す前に自動解放プールが空にならないことは保証されていますか? メソッドに入るとどうなりますか?保持しないと、someMethodThatTakesAnArray 内で使用している間に配列が消える可能性がありますね。

4

2 に答える 2

3

The autorelease pool isn't drained until the next pass of the event loop of the thread it serves. That's not going to happen as long as you're working inside your current method. So yes, array is safe for someMethodThatTakesAnArray: to use; you needn't worry.

Event management: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/Introduction/Introduction.html

Memory management: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

于 2011-07-18T14:24:47.153 に答える
1

Before your method returns, there's no opportunity for the autorelease pool to get drained. What you're doing here is fine, if you have no other need for this array than to pass it off to -someMethodThatTakesAnArray:

于 2011-07-18T14:23:56.460 に答える