在强者退出之后,为什么我的弱参考不被清除?

gui*_*eak 28 weak-references reference-counting objective-c strong-references

我有点顽固,但我想了解弱弱和强烈的参考,所以这就是为什么我再次问你.

考虑一下:

__weak NSString* mySecondPointer = myText;   
NSLog(@"myText: %@", myText);
Run Code Online (Sandbox Code Playgroud)

结果是myText: (null)非常明显的 - 弱引用在赋值后设置为null,因为没有对尖头对象的强引用.

但在这种情况下:

__strong NSString* strongPtr = [[NSString alloc] initWithFormat:@"mYTeSTteXt %d"]; 
// weak pointer points to the same object as strongPtr
__weak NSString* weakPtr = strongPtr;
if(strongPtr == weakPtr) 
     NSLog(@"They are pointing to the same obj");        
NSLog(@"StrongPtr: %@", strongPtr);
NSLog(@"weakPtr: %@", weakPtr);

NSLog(@"Setting myText to different obj or nil");

// after line below, there is no strong referecene to the created object:
strongPtr = [[NSString alloc] initWithString:@"abc"];  // or myText=nil;

if(strongPtr == weakPtr) 
     NSLog(@"Are the same");
else
     NSLog(@"Are NOT the same");
NSLog(@"StrongPtr: %@", strongPtr);
// Why weak pointer does not point to nul
NSLog(@"weakPtr: %@", weakPtr);
Run Code Online (Sandbox Code Playgroud)

输出:

2013-03-07 09:20:24.141 XMLTest[20048:207] They are pointing to the same obj
2013-03-07 09:20:24.142 XMLTest[20048:207] StrongPtr: mYTeSTteXt 3
2013-03-07 09:20:24.142 XMLTest[20048:207] weakPtr: mYTeSTteXt 3
2013-03-07 09:20:24.143 XMLTest[20048:207] Setting myText to different obj or nil
2013-03-07 09:20:24.143 XMLTest[20048:207] Are NOT the same
2013-03-07 09:20:24.144 XMLTest[20048:207] StrongPtr: abc
2013-03-07 09:20:24.144 XMLTest[20048:207] weakPtr: mYTeSTteXt 3   // <== ??
Run Code Online (Sandbox Code Playgroud)

我的问题:

为什么在strongPtr = [[NSString alloc] initWithString:@"abc"];弱指针值没有变为nil之后(为什么在开头创建的对象仍然存在于内存中,尽管它没有任何强引用? - 或者它可能有?)


我试过那个:(但是我觉得添加评论不好).我已经包含了我在@autorealesepool中创建strongPtr的代码.我不确定它是否是正确的解决方案,但它有效...

 __strong NSString* strongPtr;
    __weak NSString* weakPtr;
    @autoreleasepool {


        strongPtr = [[NSString alloc] initWithFormat:@"mYTeSTteXt %d", 3];

        // weak pointer point to object create above (there is still strong ref to this obj)
        weakPtr = strongPtr;
        if(strongPtr == weakPtr) NSLog(@"They are pointing to the same obj");        

        NSLog(@"StrongPtr: %@", strongPtr);
        NSLog(@"weakPtr: %@", weakPtr);

        NSLog(@"Setting myText to different obj or nil");   

    // after line below, there is no strong referecene to the created object:
     strongPtr = [[NSString alloc] initWithString:@"abc"];  


    }

    if(strongPtr == weakPtr) 
        NSLog(@"Are the same");
    else
        NSLog(@"Are NOT the same");
    NSLog(@"StrongPtr: %@", strongPtr);
    // Why weak pointer does not point to nul
    NSLog(@"weakPtr: %@", weakPtr);
Run Code Online (Sandbox Code Playgroud)

输出:

2013-03-07 09:58:14.601 XMLTest[20237:207] They are pointing to the same obj
2013-03-07 09:58:14.605 XMLTest[20237:207] StrongPtr: mYTeSTteXt 3
2013-03-07 09:58:14.605 XMLTest[20237:207] weakPtr: mYTeSTteXt 3
2013-03-07 09:58:14.606 XMLTest[20237:207] Setting myText to different obj or nil
2013-03-07 09:58:14.607 XMLTest[20237:207] Are NOT the same
2013-03-07 09:58:14.607 XMLTest[20237:207] StrongPtr: abc
2013-03-07 09:58:14.608 XMLTest[20237:207] weakPtr: (null)
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 17

从汇编代码可以看出,访问会weakPtr产生一个objc_loadWeak调用.

根据Clang文档,objc_loadWeak保留并自动释放对象并等同于

id objc_loadWeak(id *object) {
  return objc_autorelease(objc_loadWeakRetained(object));
}
Run Code Online (Sandbox Code Playgroud)

这(希望)解释了为什么两者兼而有之

if(strongPtr == weakPtr) ...
Run Code Online (Sandbox Code Playgroud)

NSLog(@"weakPtr: %@", weakPtr);
Run Code Online (Sandbox Code Playgroud)

创建其他自动释放的引用.

这不是一个特殊NSString问题,我可以使用自定义(普通)类重现相同的行为.