We all know:

  • __strong variables strongly reference objects, __weak variables weakly reference objects, and ownership modifiers modify variables, not objects
  • The OC method has two default arguments, id self and SEL _cmd, which can also be understood as two variables in the stack frame

The reference relationship between a variable and an object as it is passed between functions

Two controllers A and B, the following code is written in B, the operation is push from A to B, and then pop back from B, and print in THE dealloc method of B

- (void)varTest {
    __weak OneViewController *weakSelf = self;
    NSLog(@"%p-%@", &self, self);
    NSLog(@"%p-%@", &weakSelf, weakSelf);
    [self obj1:self obj2:weakSelf obj3:self obj4:weakSelf];
}

- (void)obj1:(OneViewController *)obj1 obj2:(OneViewController *)obj2 obj3:(__weak OneViewController *)obj3 obj4:(__weak OneViewController *)obj4 {
    NSLog(@"%p-%@", &self, self);
    NSLog(@"%p-%@", &obj1, obj1);
    NSLog(@"%p-%@", &obj2, obj2);
    NSLog(@"%p-%@", &obj3, obj3);
    NSLog(@"%p-%@", &obj4, obj4);
    
    self.block = ^{
//        NSLog(@"%@", obj1);
//        NSLog(@"%@", obj2);
//        NSLog(@"%@", obj3);
//        NSLog(@"%@", obj4);
    };
}
Copy the code

Obj1 receives __strong for __strong

Obj2 receives __weak for __strong // this is the point

Obj3 is __weak and receives __strong

Obj4 is __weak Receive __weak

Open 4 logs each and think about what happens…

OK! The results:

You can see

All variable addresses are different because they are referenced to the same object in different stack frames

__strong receives circular references, __weak receives no circular references

One possible omission of Obj2 is to assume that a weak reference is passed without creating a circular reference

Let’s simplify the case for obj2

Conclusion: a variable references an object. Assigning a variable to a variable makes the object reference more than once. The strength of the reference depends on the receiving variable’s ownership modifier, which is not passed!