Oc provides an internal counter for each object. This counter tracks the reference count of objects. when an object is created or copied, the reference count is 1. Each time an object is maintained, the retain interface is called, add 1 to the reference count. If you do not need this object, call release. The reference count is reduced by 1. When the reference count of the object is 0, the system will release this memory, call dealloc to release an object
When an object contains other objects, You have to release them in dealloc.
NSObject is the base class of all IOS classes.
There are two basic functions: alloc and dealloc.
Alloc is similar to C ++'s new, and dealloc is similar to delete
When the retaincount of an object is 0, the dealloc function is automatically called.
Release is just making the retaincount-1, not calling the dealloc Function
Memory Management Principles:
If you use alloc or copy to create an object, you must release
If you retain an object, you must release
Song class implementation
# Import
@ Interface Song: NSObject
{
NSString * _ title;
NSString * _ artist;
Long int _ duration;
}
@ Property (nonatomic, retain) NSString * title;
@ Property (nonatomic, retain) NSString * artist;
@ Property (nonatomic, assign) long int duration;
-(Song *) initwithTitle :( NSString *) t AndArtist :( NSString *) art AndDuration :( long int) d;
@ End
# Import "Song. h"
@ Implementation Song
@ Synthesize title = _ title;
@ Synthesize artist = _ artist;
@ Synthesize duration = _ duration;
-(Song *) initwithTitle :( NSString *) t AndArtist :( NSString *) art AndDuration :( long) d
{
Self = [super init];
If (self)
{
Self. title = t;
Self. artist = art;
Self. duration = d;
}
Return self;
}
@ End
Main Function code
Int main (int argc, const char * argv [])
{
Song * Song1 = [[Song alloc] initwithTitle: @ "what" AndArtist: @ "hello" AndDuration: 3];
Song * Song2 = [[Song alloc] initwithTitle: @ "aaa" AndArtist: @ "bbb" AndDuration: 4];
NSLog (@ "Song1 retain count is % ld", [Song1 retainCount]);
NSLog (@ "Song2 retain count is % ld", [Song2 retainCount]);
[Song1 retain];
[Song2 retain];
NSLog (@ "Song1 retain count is % ld", [Song1 retainCount]);
NSLog (@ "Song2 retain count is % ld", [Song2 retainCount]);
[Song1 release];
[Song2 release];
NSLog (@ "Song1 retain count is % ld", [Song1 retainCount]);
NSLog (@ "Song2 retain count is % ld", [Song2 retainCount]);
[Song1 release];
[Song2 release];
Return 0;
}
The result:
14:44:55. 170 Access [2891: 303] Song1 retain count is 1
14:44:55. 173 Access [2891: 303] Song2 retain count is 1
14:44:55. 173 Access [2891: 303] Song1 retain count is 2
14:44:55. 173 Access [2891: 303] Song2 retain count is 2
14:44:55. 174 Access [2891: 303] Song1 retain count is 1
14:44:55. 174 Access [2891: 303] Song2 retain count is 1
The memory management release Pool provides an object container. Every time an object sends autorelease, the reference count of the object does not change. Instead, the memory release pool records a record and records the requirements of the object, when the memory release pool sends retain or release, when the pool notifies all elements in the pool before destruction, send the release message minus 1. This part of code must be placed in:
NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
And [pool release ];
Int main (int argc, const char * argv [])
{
NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
NSArray * weeks1 = [NSArray arrayWithObjects: @ "Monday", @ "Tuesday", @ "Thursday", nil];
NSArray * weeks2 = [[NSArray alloc] initWithObjects: @ "Monday", @ "Tuesday", @ "Thursday", nil];
// [Weeks1 autorelease];
[Weeks1 release];
[Weeks2 release];
// [Weeks2 autorelease];
NSLog (@ "retain count is % ld", [weeks1 retainCount]);
NSLog (@ "retain count is % ld", [weeks2 retainCount]);
[Pool release];
Return 0;
}
Attribute Introduction
@ Property and @ synthesize can automatically generate an access method for a class member variable,
Syntax @ property (parameter) type name
The parameters here are divided into three categories:
Read/write attributes: (readwrite/readonly) readwrite: This attribute is default. readonly: Only getter generation does not have setter
Atomicity (nonatomic) atomic to ensure program concurrency and avoid synchronization problems
Memory Management: (assign/retain/copy)
Assign: This attribute is used to process basic types, such as int and float. If the declared type is basic type, this attribute can be left empty.
For assign, the set and get functions are as follows:
@ Property (nonatomic, assign) int val;
-(Int) val
{
Return val;
}
(Void) setVal :( int) newVal
{
Val = newVal;
}
Copy: automatically generate the clone of this object
The Code is as follows:
@ Property (nonatomic, copy) NSString * title;
-(NSString *) title
{
Return title;
}
-(Void) settitle :( NSString *) newtitile
{
// First, determine whether it is consistent with the old object. If it is inconsistent, assign a value.
If (newTitle! = Title)
{
[Title release];
Title = [newtitile copy];
}
}
Retain: automatically retain object to implement
The Code is as follows:
@ Property (nonatomic, retain) NSString * title;
-(NSString *) title
{
Return title;
}
-(Void) settitle :( NSString *) newtitile
{
// First, determine whether it is consistent with the old object. If it is inconsistent, assign a value.
// Execute release for the nil object, and no exception will be thrown. If nil is not performed, release releases the old object, so that no memory leakage will occur.
// If it is an object, executing the code in if will cause an extreme situation: When the retain of this name is 1, this set operation allows the Instance name to be released in advance without assigning values.
If (newTitle! = Title)
{
[Title release];
Title = [newtitile retain];
}
}
When assigning values to an attribute, the set Method of the attribute is called to add a reference count to ensure correct memory reference.
The custom class cannot use COPY because the custom class is not implemented. Protocol. There are various copy methods in this Protocol. Therefore, do not use the copy method unless necessary. Try to use it only when setting strings.
Differences between assign, retain and copy
- Copy: Create an object with an index count of 1 and release the old object.
- Retain: Release the old object, assign the value of the old object to the input object, and increase the index count of the input object to 1.
- Assign: Copy directly without creating a new object
For example, an NSString object, NSString * str = [NSString alloc] initwithString: @ "hello ";
The memory allocation process is as follows:
First, create a memory block on the stack. The content is initialized to "hello" and the address is 0X1111.
Second, create a memory on the stack. The address is 0X2222 and the content is 1111,
Assign: NSString * newStr = [str assign];
NewStr is the alias of str. The address is 0X2222, the content is 1111, And the retaincount value remains unchanged. If you delete newStr, str will also be deleted.
Copy: NSString * newStr = [str copy];
The newStr address is 0 × 3333, the content is 3333, and space is allocated on the stack. The address is 0X3333, the content is "hello", and the new object retainCount is 1, the str object remains unchanged. Deleting newStr only involves itself. retain is irrelevant to str: NSString * newStr = [str retain];The newStr address is 0X4444, And the content is 1111. The retaincount ++ of str,
Assign is a direct value assignment, which may cause problems when the data is int, float and other native types, you can use assign. Retain uses the reference count. retain causes the reference count to increase by 1. release causes the reference count to decrease by 1. When the reference count is 0, the dealloc function is called and the memory is recycled. Copy is used when you do not want a and B to share a piece of memory. A and B each have their own memory.