View the reference count of an object under Arc

Source: Internet
Author: User

The conclusion of various articles and questions and answers on the internet is that "the reference count value of the object cannot be printed under arc".

Indeed, ARC prohibits direct viewing of reference counts for Objective-c objects, but can objective-c objects be converted to core Foundation objects? So I did a little experiment:

id obj = [[NSObject alloc]init];printf("retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(obj)));

It is important to note that the __bridge is used here to convert, if it is converted with __bridge_retained, in fact, a core foundation object is generated, and the original Objective-c object is retain. Cause the reference count to be higher than the real value.

Run results

Retain count = 1

The project turns on Arc mode and correctly outputs the reference count value of the OC object.

View the reference count of an object under Arc

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.