我假设NSArrayController
的addObject和removeObject与NSMutableArray中的类似物的工作方式类似。
但是,看起来addObject会将其目标对象的refcount增加3,而NSArrayController:removeObject
只会将目标的refcount减少1,留下2的差异,这会导致代码中的内存泄漏。 / p>
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说这个对象最终会出现在一个autoreleasepool中,最终会为ArrayController耗尽。根据我的理解,autoreleasepools在runloop的末尾被排出。但是我的dealloc方法永远不会在我的演示代码中调用,即使我手动添加NSAutoreleasePool并将其耗尽。
[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:]
// 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]