Memory management and @property of iOS learning notes

Source: Internet
Author: User

After IOS5, Apple introduced the arc mechanism, which greatly facilitated the iOS developer's memory management mechanism. When iphone 4 was born why iOS can run a great game in 512M of memory and keep it flowing. Thanks to iOS's very good memory handling mechanism.

When we create a project now, the arc mechanism is introduced by default, and we can turn off the arc mechanism: Enter a long click search button in the input box

The old version of the memory operation can then be done.

In older versions, memory operations took reference counts (Retaincount) alloc retain (+1) release (-1)

Memory management principles (pairing principle): As long as there is new,alloc,retain, there must be a pairing release,autorelease

Make the introduction number +1, must correspond to-1, be sure to exist in pairs

ClassA *obj1 = [[ClassA alloc] init]; Retaincount = 1

ClassA *obj2 = obj1; Retaincount = 1

[Obj2 retain]; Retaincount = 2

When the introduction count becomes 0, the system's DEALLOC system functions are called automatically

-(void) dealloc

{

[Super dealloc];//Note Be sure to call the parent class function

NSLog (@ "object is deleted");

}

Wild hands:

In development often encounter wild pointers, the system will generally prompt for the Thread 1:exc_bad_access (code=exc_i386_gpflt) error. Because you have access to a piece of memory that is not yours.

However, we may find in the development of the system does not always detect the wild pointer, mainly to improve the efficiency of compiling, the default off the wild pointer detection mechanism, open method:

Click on the checkmark on the Enable Zombie objects to close.

Note: In general, we'd better not open this mechanism, so the efficiency of compiling will be greatly reduced.

Memory leaks:

As long as the object Retaincount! =0 is always present in memory, objects that are no longer being used, and are not destroyed in memory, can cause memory leaks.

@property parameters

1. Parameters related to the Set method memory management

Retain: To generate a set method that conforms to the memory management principles (Application and object type)

Assign: Direct Assignment (object type, base data type)

Capy

2 Multi-Threading related

Nonatomic: No multithreaded code is generated. (usually use this, high efficiency)

Atomic: Generating multithreaded Management code

3. Do you want to generate the set and get methods

ReadWrite: Readable writable property, simultaneous generation of set and get methods

ReadOnly: Read-only property, generating only get methods

4.set parameters associated with the Get method name

Setter: Sets the name of the build set method

Getter: Set the generated Get method name

Instance:

@property (Nonatomic,retain) Class *class;

@property (Nonatomic,retain) NSString *name;

@property (nonatomic,assign) int age;

@property (nonatomic,assign,readonly) int age;

@property (Nonatomic,assign,setter=abc:) int age;//Note that there is a colon

Memory management and @property of iOS learning notes

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.