Memory Management scope and @property

Source: Internet
Author: User
Tags define null

Scope of Management:

Manages any inherited NSObject objects that are not valid for other basic data types

The intrinsic reason is because the object and other data types in the system storage space is not the same, the other local variables are mainly stored in the stack, and the object is stored in the heap, when the code block at the end of the code block involved in all the local variables will be recycled, the pointer to the object is also recycled, the object has no pointer to point, But it still exists in memory, causing a memory leak.

Principles of memory management

1) Principles  

As long as someone else is using an object, the object will not be recycled; As long as you want to use this object, then you should let the object reference counter +1; When you do not want to use this object, you should let the object reference counter-1;

2) who created , who Release

(1) If you create an object by Alloc,new,copy, you must call the release or Autorelease method
(2) You're not the one who created it.

3) who retain, who Release

As long as you call the retain, no matter how the object is generated, you have to call release

4) Summary  

After the beginning and finish, there should be a reduction in addition. Once you have an object counter plus 1, you should make it last-1.

:

1) NULL pointer: There is no pointer to anything, sending a message to a null pointer does not give an error about the difference between nil and nil and null:

NIL:A null pointer to an OBJECTIVE-C object. (#define NIL ((ID) 0)) nil is an object value.
NIL:A null pointer to an OBJECTIVE-C class.

NULL:A null pointer to anything else. (#define NULL ((void *) 0)) Null is a general-purpose pointer (generic pointer).

Nsnull:a class defines A Singleton object used to represent null values in collection objects (which don ' t allow Nil values).

[NSNull NULL]: The singleton instance of NSNull.

[NSNull null] is an object that he uses in situations where nil can not be used.

@property

@property int age; 1) generate a _age 2) generate the Get and set method declaration 3) generate the Get and set method implementation

@property (...) int age;

1) memory Management related parameters  

Retain: Release old value for object, retain new value (for OC object type) Assign: Direct assignment (default, for non-OC object type) Copy:release old value, copy new value

Verify that assign is actually directly assigned if it works on an object//use @property enhanced to generate get and set methods

@property (nonatomic,assign) Car *car; /*

What is actually generated in the. m file is
_car = car; When the object is release, the object cannot be used

*/

@property Parameters (ii)

1) do you want to build Set Method ( not generated if it is a read-only property )

ReadOnly: Read only, generate getter declarations and implementations ReadWrite: default, generate both setter and getter declarations and implementations

2) Multithreading Management ( Apple has blocked multithreaded operations to some extent )

Nonatomic: High performance, generally use this atomic: low performance, default

3) name of the set and get methods

Modifies the name of the set and get methods, primarily for Boolean types. Because a method name that returns a Boolean type is typically preceded by an IS, modifying a getter whose name is typically used in a Boolean type.

@property (nonatomic,assign, setter=abc:,getter=haha) int age can be understood as putting [psetage:]------>[PABC:], [page]---------> [Phaha];

@property (Nonatomic,assign, SETTER=SETVIP:,GETTER=ISVIP) BOOL VIP;

Memory Management scope and @property

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.