Why variable with __weak qualifier retains an obje

2019-02-18 07:32发布

Here is my code:

extern void _objc_autoreleasePoolPrint();

int main(int argc, const char * argv[])
{

    NSArray __weak *tmp = nil;

    @autoreleasepool {

        NSArray __strong *obj = [[NSArray alloc] init];

        NSLog(@"obj &: %p", obj);

        tmp = obj;

        NSLog(@"tmp &: %p", tmp);

        _objc_autoreleasePoolPrint();
    }

    NSLog(@"tmp: %@", tmp); // why not (null) ?


    return 0;
}

And console output:

    2013-05-01 22:14:32.966 SimpleConsoleObjectiveCApplicationWithARC[40660:f07] obj &: 0x7fedf9403110
2013-05-01 22:14:32.969 SimpleConsoleObjectiveCApplicationWithARC[40660:f07] tmp &: 0x7fedf9403110
objc[40660]: ##############
objc[40660]: AUTORELEASE POOLS for thread 0x7fff751af180
objc[40660]: 2 releases pending.
objc[40660]: [0x7fedf9805000]  ................  PAGE  (hot) (cold)
objc[40660]: [0x7fedf9805038]  ################  POOL 0x7fedf9805038
objc[40660]: [0x7fedf9805040]    0x7fedf9403110  __NSArrayI
objc[40660]: ##############
2013-05-01 22:14:32.971 SimpleConsoleObjectiveCApplicationWithARC[40660:f07] tmp: (
)

PS #1

Changed NSArray to NSMutableArray and tmp variable became nil.

extern void _objc_autoreleasePoolPrint();

int main(int argc, const char * argv[])
{

    NSMutableArray __weak *tmp = nil;

    @autoreleasepool {

        NSMutableArray __strong *obj = [[NSMutableArray alloc] init];

        NSLog(@"obj &: %p", obj);

        tmp = obj;

        NSLog(@"tmp &: %p", tmp);

        _objc_autoreleasePoolPrint();
    }

    NSLog(@"tmp: %@", tmp);


    return 0;
}

Can somebody explain me why it works this way?

1条回答
beautiful°
2楼-- · 2019-02-18 07:58

It seems that [[NSArray alloc] init] returns a "shared instance" of an empty NSArray:

NSArray *a = [[NSArray alloc] init];
NSArray *b = [[NSArray alloc] init];
NSLog(@"a &: %p", a);
NSLog(@"b &: %p", b);

Output:

    a &: 0x100103110
    b &: 0x100103110

This "shared instance" continues to exist even if your strong reference obj is gone, therefore the weak pointer is not set to nil.

Obviously, [[NSMutableArray alloc] init] cannot return a shared instance.

查看更多
登录 后发表回答