我有一个弱的NSPointerArray和一些已经发布的NSObject。在调用compact之前,我看到的是:
(lldb) po [currentArray count]
1
(lldb) po [currentArray pointerAtIndex:0]
<nil>
(lldb) po [currentArray allObjects]
<__NSArrayM 0x16f04f00>(
)这是有道理的,但真正奇怪的是,当我在那个数组上调用compact时,我会看到相同的值!Count仍然返回1,而pointerAtIndex:0是nil。
为什么零没有被移除?
编辑
下面是完整的代码(是的,这是XCTesting框架):
- (void)testCompaction {
__weak id testingPointer = nil;
NSPointerArray *weakArray = [NSPointerArray weakObjectsPointerArray];
@autoreleasepool {
NSObject *someObj = [[NSObject alloc] init];
testingPointer = someObj;
[weakArray addPointer:(__bridge void*)testingPointer];
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]);
someObj = nil;
}
NSLog(@"before compaction outside autorelease: testingPointer = %@ count = %d, allObjects = %@, pointerAtIndex:0 = %@, pointerAtIndex:0 class = %@", testingPointer, [weakArray count], [weakArray allObjects], [weakArray pointerAtIndex:0], [(id)[weakArray pointerAtIndex:0] class]);
[weakArray compact];
NSLog(@"after compaction outside autorelease: testingPointer = %@ count = %d, allObjects = %@, pointerAtIndex:0 = %@, pointerAtIndex:0 class = %@", testingPointer, [weakArray count], [weakArray allObjects], [weakArray pointerAtIndex:0], [(id)[weakArray pointerAtIndex:0] class]);
}和原木:
before compaction inside autorelease: testingPointer = <NSObject: 0x7de7ff80> count = 1, allObjects = (
"<NSObject: 0x7de7ff80>"
), pointerAtIndex:0 = <NSObject: 0x7de7ff80>, pointerAtIndex:0 class = NSObject
2015-07-20 14:27:14.062 AppetizeSuite copy[54144:9019054] before compaction outside autorelease: testingPointer = (null) count = 1, allObjects = (
), pointerAtIndex:0 = (null), pointerAtIndex:0 class = (null)
2015-07-20 14:27:22.615 AppetizeSuite copy[54144:9019054] after compaction outside autorelease: testingPointer = (null) count = 1, allObjects = (
), pointerAtIndex:0 = (null), pointerAtIndex:0 class = (null) 为什么compact方法不删除第一个指针?在调用nil之前,这显然是一个compact。
发布于 2015-07-27 14:55:37
我也见过同样的行为。下面是一个开放的雷达漏洞报告:http://www.openradar.me/15396578
发布于 2016-10-27 00:58:04
发生这种情况的原因是-compact首先检查是否设置了内部标志'needsCompaction‘。如果不是的话,它就会提前出航。设置标志的唯一时间是通过公共接口将零指针直接插入到数组中。如果弱引用对象在指针插入数组后被解除分配(指针为零),则不会设置该对象。
要解决此问题,一项工作是在调用-compact之前,有目的地向数组追加一个零指针。不太理想,但会成功的。
[pa addPointer:nil]; // forces the pointer array to do compaction next time
[pa compact];发布于 2020-05-31 11:14:58
已经过了几年了,使用示例代码没有任何变化。
但是,当使用方法-compact执行指针更新时,-replacePointerAtIndex:withPointer工作得很好:
在上面的代码中,替换
someObj = nil;通过
[weakArray replacePointerAtIndex:0 withPointer:nil];提供[weakArray compact];后的预期结果,代码将在最后一个NSLog崩溃。
https://stackoverflow.com/questions/31322290
复制相似问题