Properties of memory management and @property

Source: Internet
Author: User

Properties of memory management and @property

Directory

    • Understanding of memory management
    • Management of Memory

Ownership and memory management principles for objects

Rationally solve the problems caused by memory management

Auto Free Pool

    • Properties of the @property

Understanding of memory management

Management of Memory

Ownership and memory management principles for objects

Both an object and a function can have administrative rights on the class object, so objects and functions need to be responsible for creating and releasing class objects

A reasonable solution to the problems caused by memory management (in the MRC, there is a release of the owning object on the Dealloc)

Stage One

MyClass *myclass = [[MyClass alloc] init];

MyClass's behavior

[MyClass release];

Phase II

-(void) Setengine: (engine *) newengine{engine = Newengine;}

Car *car = [[Car alloc] init];

Engine *engine = [[Engine alloc] init]; 1

[Car setengine:engine]; 1

[Engine release]; 0 causes the engine in the car object to point to an empty object

Stage Three

Adjustment to Setter:-(void) Setengine: (engine *) newengine{engine = [newengine retain];};

A problem exists:

Engine *engine1 = [engine new]; 1

[Car setengine:engine1]; 2

[Engine1 release]; 1

Engine *engine2 = [engine new]; 1

[Car setengine:engine2]; 1

[Car release]; 1 The Engine1 is not released at this time, that is, the object that the car originally pointed to was not released

Stage Three

Continue adjustment to setter:-(void) Setengine: (engine *) newengine{[engine release]; engine = [newengine retain]; }

There is a problem

Engine *engine = [engine new]; 1

Car *car1 = [car new];

Car *car2 = [car new];

[Car1 Setengine:engine]; 2

[Engine release]; 1

[Car2 SETENGINE:[CAR1 engine]]; Error at this point the engine's reference count is 0,CAR1 and CAR2 cannot use its Engine property

Stage Four

Continue adjustment to setter:-(void) Setengine: (Engine *) newengine{[newengine retain]; [Engine release];  engine = Newengine; }

Auto Free Pool

Properties of the @property

Properties for memory management 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.