IOS MRC Development

Source: Internet
Author: User

1. Assign means direct assignment, retain means release old value, retain new value 1.1 @property (assign, nonatomic) UIWindow *window; means:

-(void) SetWindow: (UIWindow *) window

{

_window = window;

}

    

//APPDELEGATE.M

Because it is assign, there is no retain operation on the objects produced by [[UIWindow Alloc]init]. When the auto-release pool is released, the reference counter changes from 1 to 0,appdelegate cannot have the window for a long time

Self.window = [[[UIWindow alloc]init]autorelease];

//Since the automatic release of the pool release, the resulting window will be destroyed, do not add autorelease it? This does not conform to the memory management principle, which means that the resulting window can never be destroyed

Self.window.backgroundColor = [Uicolor Redcolor];

Self.window.frame = [UIScreen mainscreen].bounds;

[Self.window makekeyandvisible];

return YES;

   

//If you use the following to modify the above code for the following, it seems that appdelegate can have long-term window,appdelegate release, can also destroy window

Self.window = [[UIWindow alloc]init];

//The potential problem is that if you assign a value to Self.window repeatedly, for example, write a line of code Self.window = [[UIWindow alloc]init]. The old window cannot be destroyed when the Appdelegate is released

Self.window.backgroundColor = [Uicolor Redcolor];

Self.window.frame = [UIScreen mainscreen].bounds;

[Self.window makekeyandvisible];

return YES;

-(void) dealloc{

[Self.window release];

[Super Dealloc];

}

1.2 @property (retain, nonatomic) UIWindow *window; means:

-(void) SetWindow: (UIWindow *) window

{

if (_window! = window) {

[_window release];

_window = [window retain];

}

}

    

Appdelegate.m

Because it is a retain, the object generated by [[UIWindow Alloc]init] is retain, and when freed by the auto-release pool, it is changed from 2 to 1,appdelegate can have the window for a long time

Self.window = [[[UIWindow alloc]init]autorelease];

Self.window.backgroundColor = [Uicolor Redcolor];

Self.window.frame = [UIScreen mainscreen].bounds;

[Self.window makekeyandvisible];

return YES;

   In order to prevent memory leaks, when the Appdelegte is destroyed, the owning window should be the release operation

-(void) dealloc{

[Self.window release];

[Super Dealloc];

}

  

2. Origin of non-ARC memory Management 2.1 Prepare:

-------------Dog----------Begin-------------

#import <Foundation/Foundation.h>

@interface Dog:nsobject

@end

#import "Dog.h"

@implementation Dog

-(void) dealloc

{

NSLog (@ "Dog--dealloc");

[Super Dealloc];

}

@end

-------------Dog----------End-------------

-------------person----------Begin-------------

#import <Foundation/Foundation.h>

@class Dog;

@interface Person:nsobject

{

Dog *_dog;

}

-(void) Setdog: (dog *) Dog;

-(dog *) dog;

@end

#import "Person.h"

@implementation person

-(void) Setdog: (Dog *) dog

{

_dog = dog;

}

-(dog *) dog

{

return _dog;

}

-(void) dealloc

{

NSLog (@ "Person--dealloc");

[Super Dealloc];

}

@end

-------------person----------End-------------

 2.2 Original version

-------------Mainviewcontroller----------Begin-------------

Dog *d = [[Dog alloc]init];

Person *p =[[person Alloc]init];

P.dog = D;     

[D release];

NSLog (@ "p.dog----%@", p.dog); //Zombie Object

[P release];

-------------Mainviewcontroller----------End-------------

  2.3 Optimization to solve zombie object problems

Modify the Set method for person

-(void) Setdog: (Dog *) dog

{

_dog = [dog retain]; //So Mainviewcontroller will not access zombie objects

}

-(void) dealloc

{

NSLog (@ "Person--dealloc");

[Self.dog release]; //Who retain, who release

[Super Dealloc];

}

  2.4 Problems: When a person destroys a duplicate assignment, only the latest held dog is released, and the old dog memory leaks

Dog *d = [[Dog alloc]init];

Dog *D2 = [[Dog alloc]init];

Person *p =[[person Alloc]init];

P.dog = D;

P.dog = D2;

[D release];

NSLog (@ "p.dog----%@", p.dog);

[P release];

2.5 Optimization: Solve the problem of duplicate assignment

-(void) Setdog: (Dog *) dog

{

[_dog release];

_dog = [dog retain];

}

2.6 Problems: Duplicate assignment of the same dog, there will be a wild pointer error

Dog *d = [[Dog alloc]init];

Person *p =[[person Alloc]init];

P.dog = D;

P.dog = D; a wild pointer error occurs when executing to this line

[D release];

NSLog (@ "p.dog----%@", p.dog);

[P release];

2.7 Optimization: In Set method plus judgment

-(void) Setdog: (Dog *) dog

{

if (_dog! = dog) {

[_dog release];

_dog = [dog retain];

}

}

3. The attribute is equivalent to generating a 2.6 optimization Set/get method after the retain, if the set method after assign,2.6 optimization is also written, so that the object can hold the property for a long time3.1 modifying Person.h

@interface Person:nsobject

@property (retain,nonatomic) Dog *dog;

@end

3.2 Modify Person.m,dog's Set method and get can remove 4. Summarize, do non-arc development

1> to hold a property for a long time with retain. Note: In general, attributes, in addition to UI controls, are typically held for long periods of time

2> in the Dealloc method, to release:

Writing one: [Self. property name release];

_ Property name = Nil;

Notation Two: [_ Property name release];

_ Property name = Nil;

Three: [self. Attribute name Nil];

5.

IOS MRC Development

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.