objective-c – NSPointerArray奇怪的压缩

前端之家收集整理的这篇文章主要介绍了objective-c – NSPointerArray奇怪的压缩前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个很弱的NSPointerArray与一些已经被释放的NSObject.在打电话之前,我看到的是:
  1. (lldb) po [currentArray count]
  2. 1
  3. (lldb) po [currentArray pointerAtIndex:0]
  4. <nil>
  5. (lldb) po [currentArray allObjects]
  6. <__NSArrayM 0x16f04f00>(
  7.  
  8. )

这是有道理的,但真正奇怪的是,当我在该数组上调用compact时,我看到相同的值! Count仍然返回1,而pointerAtIndex:0为零.

为什么没有被删除

编辑

这是完整的代码(是的,它是XCTesting框架):

  1. - (void)testCompaction {
  2. __weak id testingPointer = nil;
  3.  
  4. NSPointerArray *weakArray = [NSPointerArray weakObjectsPointerArray];
  5.  
  6. @autoreleasepool {
  7.  
  8. NSObject *someObj = [[NSObject alloc] init];
  9.  
  10. testingPointer = someObj;
  11.  
  12. [weakArray addPointer:(__bridge void*)testingPointer];
  13.  
  14. NSLog(@"before compaction inside autorelease: testingPointer = %@ count = %d,allObjects = %@,pointerAtIndex:0 = %@,pointerAtIndex:0 class = %@",testingPointer,[weakArray count],[weakArray allObjects],[weakArray pointerAtIndex:0],[(id)[weakArray pointerAtIndex:0] class]);
  15.  
  16. someObj = nil;
  17. }
  18.  
  19. NSLog(@"before compaction outside autorelease: testingPointer = %@ count = %d,[(id)[weakArray pointerAtIndex:0] class]);
  20.  
  21. [weakArray compact];
  22.  
  23. NSLog(@"after compaction outside autorelease: testingPointer = %@ count = %d,[(id)[weakArray pointerAtIndex:0] class]);
  24. }

和日志:

  1. before compaction inside autorelease: testingPointer = <NSObject: 0x7de7ff80> count = 1,allObjects = (
  2. "<NSObject: 0x7de7ff80>"
  3. ),pointerAtIndex:0 = <NSObject: 0x7de7ff80>,pointerAtIndex:0 class = NSObject
  4. 2015-07-20 14:27:14.062 AppetizeSuite copy[54144:9019054] before compaction outside autorelease: testingPointer = (null) count = 1,allObjects = (
  5. ),pointerAtIndex:0 = (null),pointerAtIndex:0 class = (null)
  6. 2015-07-20 14:27:22.615 AppetizeSuite copy[54144:9019054] after compaction outside autorelease: testingPointer = (null) count = 1,pointerAtIndex:0 class = (null)

为什么compact方法删除第一个指针?在打电话前显然是零.

解决方法

我看到了同样的行为.这是一个开放的雷达错误报告:
http://www.openradar.me/15396578

猜你在找的C&C++相关文章