NSArrayController
のaddObjectとremoveObjectは、NSMutableArrayの類似物と同様に機能すると想定しました。
ただし、addObjectはターゲットオブジェクトのrefcountを3増やしNSArrayController:removeObject
、ターゲットのrefcountを1だけ減らし、2の不一致を残しているように見えます。これにより、コードでメモリリークが発生します。
NSArrayController *ac = [[NSArrayController alloc] init];
NSMutableArray *ma = [[NSMutableArray alloc] initWithCapacity:3];
Custom *obj = [[Custom alloc] init];
NSLog(@"Initial: %lu", [obj retainCount]);
[ma addObject:obj];
NSLog(@"After NSMutableArray:addObject: %lu", [obj retainCount]);
[ma removeObject:obj];
NSLog(@"After NSMutableArray:removeObject: %lu", [obj retainCount]);
[ac addObject:obj];
NSLog(@"After NSArrayController:addObject: %lu", [obj retainCount]);
[ac removeObject:obj];
NSLog(@"After NSArrayController:removeObject: %lu", [obj retainCount]);
[obj release];
NSLog(@"End: %lu", [obj retainCount]);
私が期待したもの:
2013-02-10 23:11:22.344 Demo[27208:303] Initial: 1
2013-02-10 23:11:22.345 Demo[27208:303] After NSMutableArray:addObject: 2
2013-02-10 23:11:22.345 Demo[27208:303] After NSMutableArray:removeObject: 1
2013-02-10 23:11:22.345 Demo[27208:303] After NSArrayController:addObject: 2
2013-02-10 23:11:22.345 Demo[27208:303] After NSArrayController:removeObject: 1
2013-02-10 23:11:22.345 Demo[27208:303] End: 0
私が実際に見ているもの:
2013-02-10 23:11:22.344 Demo[27208:303] Initial: 1
2013-02-10 23:11:22.345 Demo[27208:303] After NSMutableArray:addObject: 2
2013-02-10 23:11:22.345 Demo[27208:303] After NSMutableArray:removeObject: 1
2013-02-10 23:11:22.345 Demo[27208:303] After NSArrayController:addObject: 4
2013-02-10 23:11:22.345 Demo[27208:303] After NSArrayController:removeObject: 3
2013-02-10 23:11:22.345 Demo[27208:303] End: 2
NSArrayController
間違って使用しているだけですか?
編集
これがInstrumentsの結果です。Instrumentsによると、オブジェクトは最終的に自動リリースプールに入れられ、最終的にはArrayControllerに排出されます。私が理解していることから、自動リリースプールは実行ループの最後に排出されます。ただし、NSAutoreleasePoolを手動で追加してドレインした場合でも、deallocメソッドがデモコードで呼び出されることはありません。
NSMutableArray
コード
[ma addObject:obj];
NSLog(@"After NSMutableArray:addObject: %lu", [obj retainCount]);
[ma removeObject:obj];
NSLog(@"After NSMutableArray:removeObject: %lu", [obj retainCount]);
プロフィール
# Address Category Event Type RefCt Timestamp Size Responsible Library Responsible Caller
0 0x7fc02c80d170 Custom Malloc 1 00:00.363.486 16 Demo -[AppDelegate applicationDidFinishLaunching:]
1 0x7fc02c80d170 Custom Retain 2 00:00.364.189 0 Demo -[AppDelegate applicationDidFinishLaunching:]
2 0x7fc02c80d170 Custom Retain 3 00:00.364.502 0 Demo -[AppDelegate applicationDidFinishLaunching:]
3 0x7fc02c80d170 Custom Release 2 00:00.364.503 0 Demo -[AppDelegate applicationDidFinishLaunching:]
4 0x7fc02c80d170 Custom Release 1 00:00.364.505 0 Demo -[AppDelegate applicationDidFinishLaunching:]
5 0x7fc02c80d170 Custom Release 0 00:00.364.852 0 Foundation -[NSNotificationCenter postNotificationName:object:userInfo:]
NSArrayController
コード
// NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[ac addObject:obj];
NSLog(@"After NSArrayController:addObject: %lu", [obj retainCount]);
[ac removeObject:obj];
NSLog(@"After NSArrayController:removeObject: %lu", [obj retainCount]);
// [pool drain];
プロフィール
# Address Category Event Type RefCt Timestamp Size Responsible Library Responsible Caller
0 0x7ff1e923b8d0 Custom Malloc 1 00:00.389.904 16 Demo -[AppDelegate applicationDidFinishLaunching:]
1 0x7ff1e923b8d0 Custom Retain 2 00:00.390.534 0 AppKit -[NSArrayController _insertObject:atArrangedObjectIndex:objectHandler:]
2 0x7ff1e923b8d0 Custom Retain 3 00:00.390.554 0 AppKit -[_NSModelObservingTracker _startObservingModelObject:]
3 0x7ff1e923b8d0 Custom Retain 4 00:00.390.576 0 AppKit -[NSArrayController selectedObjects]
4 0x7ff1e923b8d0 Custom Retain 5 00:00.390.948 0 AppKit -[NSArrayController removeObject:]
5 0x7ff1e923b8d0 Custom Release 4 00:00.390.977 0 AppKit -[_NSModelObservingTracker _stopObservingModelObject:]
6 0x7ff1e923b8d0 Custom Release 3 00:00.390.984 0 AppKit -[NSArrayController _removeObjectsAtArrangedObjectIndexes:contentIndexes:objectHandler:]
7 0x7ff1e923b8d0 Custom Release 2 00:00.391.387 0 Foundation -[NSNotificationCenter postNotificationName:object:userInfo:]
8 0x7ff1e923b8d0 Custom Release 1 00:00.395.916 0 Foundation -[NSAutoreleasePool drain]
9 0x7ff1e923b8d0 Custom Release 0 00:00.395.919 0 Foundation -[NSAutoreleasePool drain]