Manage memory manually
The retain count is a fairly simple concept in which an object in Objective-c has a retain count. The retain count is an integer. When you use the Alloc function to create an object, the retain count for that object is set to 1. When the count becomes 0, the object is released. The retain message is generally sent to the object, thereby increasing the retain count of the object. Sends the release to the object, reducing the value of the retain count.
Before manually managing memory, go to the "Build settings" of the project and find the "Objective-c Automatic Reference Counting" set to "No"
For example, we want to perform manual memory management on a person class:
Interface file
Person.h
#import <Foundation/Foundation.h>
@interface Person:nsobject
{
NSString *_name;
}
-(void) SetName: (NSString *) name;
-(NSString *) name;
@end
Implementation file:
Person.m
#import "Person.h"
@implementation person
-(void) SetName: (NSString *) name
{
_name = name;
}
-(NSString *) name
{
return _name;
}
-(void) dealloc
{
NSLog (@ "Memory cleanup.");
[Super Dealloc];
}
@end
Main file:
Main.m
#import <Foundation/Foundation.h>
#import "Person.h"
int main (int argc, const char * argv[]) {
@autoreleasepool {
Person *person = [[Person alloc] init];
[Person setname:@ "Mr. Persimmon"];
NSString *getname = [person name];
Counter-1
[Person release];
[GetName release];
NSLog (@ "The name is%@", getName);
}
return 0;
}
Accessor method
When a variable is a regular int type method, you can create a simple accessor method
Get method
-(int) Food
{
return food;
}
Set method
-(void) Setfood: (int) x
{
food = x;
}
But when a variable is a pointer type, you need to use the normal 3 usage.
First usage: retain, then release
Objective-c
-(void) Setfood: (NSDate *) x
{
[x retain];
[Food release];
food = x;
}
Note: If these two objects point to the same object, then retain and release are superfluous.
Second usage: check before changing
-(void) Setfood: (NSDate *) x
{
if (food!= x) {
[Food release];
food = [x retain];
}
}
Flaw: An extra if statement must be executed once.
The third usage: automatically releasing old objects
Objective-c
-(void) Setfood: (NSDate *) x
{
[Food autorelease];
food = [x retain];
}
Note: If there is a retain count error, you must wait for the end of the loop to find, this is not conducive to tracking. The first two methods are easy to find when a program crashes.
ARC Memory Management
Although manual reference counting is simple, it is difficult to expect the program to execute smoothly. In fact, such a method reduces the memory leak, but it causes the program to crash, the problem of error.
With arc, we will no longer need to focus on the object's retain count, and two will focus more on the relationships of these objects.
The relationship between objects is a reference, and there are 2 types of reference type, namely strong and weak references.
Strong references
By default, references are strong references.
We just need to use it as usual, arc solves the problem of the release of strong references in Dealloc, and we can use the Dealloc method to solve other memory cleanup problems.
Weak reference
A weak reference is similar to the old manual reference count pointer: There is no implicit retain, and the pointer value is modified only in memory.
However, such references have been the cause of the crash of the program. If the pointer is not retain, the object is assigned, then a zombie pointer is left behind, which is a potential cause of collapse.
The arc solves this problem when the pointer points to an object that is reassigned, and the field Chiang-weak reference is set to nil, which is called the "zeroing Weak reference."
When do I use weak references?
When two objects are encountered retain each other, the end result will never be released. This is called a strong reference loop in the arc.
How to avoid it?
By using weak references with skill, you can avoid such a reference loop.
@interface Person:nsobject
{
Person *parent; Wrong ~, which led to a strong reference loop
Nsmutablearray *children;
}
@end