Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPointerArray weird compaction

I have a weak NSPointerArray with some NSObject that has been released. Before calling compact what I see is:

(lldb) po [currentArray count]
1
(lldb) po [currentArray pointerAtIndex:0]
<nil>
(lldb) po [currentArray allObjects]
<__NSArrayM 0x16f04f00>(

)

That makes sense, but what is really weird is that when I call compact on that array I see the same values! Count still returns 1 and pointerAtIndex:0 is nil.

Why the nil hasn't been removed?

EDIT

Here's the full code (yeah it's XCTesting framework):

- (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]);
}

and logs:

  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)   

Why the compact method does not delete the first pointer? It's clearly a nil before calling compact.

like image 322
Łukasz Sromek Avatar asked Jul 09 '15 15:07

Łukasz Sromek


2 Answers

The reason this happens is that -compact first checks whether an internal flag 'needsCompaction' is set. If it's not, it simply bails early. The only time the flag is set is if a nil pointer is inserted directly into the array through the public interface. It does not get set if a weakly referenced object is deallocated (and the pointer is nil'd) after the pointer was inserted into the array.

One work around for this behavior is to purposefully append a nil pointer to the array before calling -compact. Not ideal but it'll work.

[pa addPointer:nil]; // forces the pointer array to do compaction next time
[pa compact];
like image 54
seth Avatar answered Nov 14 '22 00:11

seth


I've seen this same behavior. Here is an open radar bug report: http://www.openradar.me/15396578

like image 44
Will Lisac Avatar answered Nov 14 '22 00:11

Will Lisac