Retain and release of Object-C (retention and release)

Source: Internet
Author: User

In object-C, the system automatically saves a reference counter for each created object. When an object is created, the reference count is set to 1. Each time the object must be kept, you need to send a (called) Retain to add 1 to the reference count. When you no longer need an object, you can send a release message to reduce the reference count by 1. When the reference count is 0, the system will release its memory (by sending the dealloc message to the object, that is, truly revoking the object's memory ). In addition, you can use retaincount to obtain the reference count of this object. The returned type is an nsuinteger. The basic call method is as follows:
[OBJ retain];
[OBJ release];
[OBJ retaincount];

1. ExampleCode:
// 1. classa. h
# Import <Foundation/nsobject. h>

@ Interface classa: nsobject

@ End

// 2. classa. m
# Import "classa. H"

@ Implementation classa

// Declaration in. H. You do not need to declare the dealloc method.
-(Void) dealloc {
Printf ("deallocing classa \ n ");
[Super dealloc];
}
@ End

// 3. Main. m
# Import <stdio. h>
# Import "classa. H"

Int main (INT argc, const char * argv []) {
Printf ("Test Case 1: \ n ");
Classa * a1 = [[classa alloc] init];
Classa * a2 = [[classa alloc] init];

Printf ("A1 retain count: % I \ n", [a1 retaincount]);
Printf ("A2 retain count: % I \ n", [A2 retaincount]);

Printf ("Test Case 2: \ n ");
[A1 retain]; // 2
[A1 retain]; // 3
[A2 retain]; // 2

Printf ("A1 retain count: % I \ n", [a1 retaincount]);
Printf ("A2 retain count: % I \ n", [A2 retaincount]);

Printf ("Test Case 3: \ n ");
[A1 release]; // 2
[A2 release]; // 1

Printf ("A1 retain count: % I \ n", [a1 retaincount]);
Printf ("A2 retain count: % I \ n", [A2 retaincount]);

// Release
[A1 release]; // 1 // note that there must be several release records.
[A1 release]; // 0 // and only call delloc once
[A2 release]; // 0

Return 0;
}

2. Compile and run:
Gcc-fconstant-string-class = nsconstantstring-C main. M-I/gnustep/system/library/Headers
Gcc-C classa. M-I/gnustep/system/library/Headers
GCC main. O classa. O-o main-L/gnustep/system/library/libraries/-lobjc-lgnustep-Base

$./Main.exe
Test Case 1:
A1 retain count: 1
A2 retain count: 1
Test Case 2:
A1 retain count: 3
A2 retain count: 2
Test Case 3:
A1 retain count: 2
A2 retain count: 1
Deallocing classa
Deallocing classa
 
3. Description:
(1). There are several restyles, there must be several release, and delloc will only be called once;
(2 ). one principle is that you have to release the memory you have applied for. Instead of applying for it, please do not release it at will. This is not your responsibility;
(3) Each time a retain object is retained, it should be release or autoreleas.

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.