Questions about iOS _ weak learning, ios _ weak

Source: Internet
Author: User

Questions about iOS _ weak learning, ios _ weak


_ Weak references do not hold objects, so the variable assigned to the _ weak modifier does not change the counter value.

Main. m

Id _ strong obj3 = nil;

Id _ weak obj1 = nil;

/*

Id _ weak obj1 = obj;

The simulation code of the compiler is as follows:

Id obj1;

Objc_iniitWeak (& obj1, obj );

Objc_destroyWeak (& obj1 );

*/

@ Autoreleasepool {

Id obj = [[NSObjectalloc] init];

Obj1 = obj;

Obj3 = obj;

NSLog (@ "% d", _ objc_autoreleasePoolPrint (); // view the content in the POOL under the ARC Mechanism

NSLog (@ "obj retainCount = % ld", CFGetRetainCount (_ bridgeCFTypeRef) obj ));

NSLog (@ "obj1 retainCount = % ld", CFGetRetainCount (_ bridgeCFTypeRef) obj1 ));

NSLog (@ "obj3 retainCount = % ld", CFGetRetainCount (_ bridgeCFTypeRef) obj3 ));

}

NSLog (@ "obj3 retainCount = % ld", CFGetRetainCount (_ bridgeCFTypeRef) (obj3 )));

NSLog (@ "obj1 retainCount = % ld", CFGetRetainCount (_ bridgeCFTypeRef) (obj1 )));

NSLog (@ "% d", _ objc_autoreleasePoolPrint ());


/*

Print as follows:

Objc [1029]: ##############

Objc [1029]: autorel?pools for thread 0x7fff7455a300

Objc [1029]: 1 releases pending.

Objc [1029]: [0x101001000] ...... PAGE (hot) (cold)

Objc [1029]: [0x101001038] ############### POOL 0x101001038

Objc [1029]: ##############

23:02:39. 686 Objective-CObject andCore FoundationObject[1029:100223] 139077936

23:02:39. 687 Objective-CObject andCore FoundationObject[1029:100223] obj retainCount = 2 -------- >>>>>>> (1)

23:02:39. 688 Objective-CObject andCore FoundationObject[1029:100223] obj1 retainCount = 3-------- >>>>>>> (2)

23:02:39. 688 Objective-CObject andCore FoundationObject[1029:100223] obj3 retainCount = 2-------- >>>>>>> (1)

23:02:39. 688 Objective-CObject andCore FoundationObject[1029:100223] obj3 retainCount = 1-------- >>>>>>> (3)

23:02:39. 688 Objective-CObject andCore FoundationObject[1029:100223] obj1 retainCount = 2-------- >>>>>>> (4)

Objc [1029]: ##############

Objc [1029]: autorel?pools for thread 0x7fff7455a300

Objc [1029]: 0 releases pending.

Objc [1029]: [0x101001000] ...... PAGE (hot) (cold)

Objc [1029]: ##############

23:02:39. 688 Objective-CObject andCore FoundationObject[1029:100223] 139077936


*/

/*

1: The variable modified by the _ weak modifier (1) above does not hold the object.

2: (3) it can be seen that when the _ strong modified variable exceeds the scope, it does not hold the object, resulting in rerainCout-1;

3: careful readers may have discovered that the retainCount of obj1 is different from that of obj? (This is my biggest question)

The following describes the author's thinking process.

(1) Although the _ weak modified variable is a weak reference to the object, it does not change the reference count (retainCount) of the value assignment object (obj), but its retainCount will increase. -------- >>> but they direct to the same address. Why is retainCount different? So this road is disconnected.

(2) Although they are the same address, _ weak does not hold objects, but the _ weak variable must make retainCount + 1 to use objects, but it does not change the retainCount of the object. with this question, I finally found the answer --> when looking for an object through the _ weak pointer, it has a modifier, when using this parameter, the returned retainCount + 1 will be returned (note that this is not the retainCount itself ).

4: holding an object is to see if it causes retainCount + 1 of the object, rather than whether it points to the address.

*/


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.