iOS Development app Value Dealloc What should I write?

Source: Internet
Author: User

In a non-ARC development environment, Dealloc is the last chance to clean up memory before the class is released. In the end, those variables and attributes should be released, and some special classes (Nstimer,observer) should be released. It is important to note that not releasing can cause memory leaks, too much release can cause memory leaks, and then slowly unfold:

1 release of variables

Variable declaration


@interface enterhondaviewcontroller:uiviewcontroller{

Uiimageview * IMAGELOGO;

UIButton * btn_corporate;

UIButton * Btn_brand;


Corporateview * Corporateview;

Brandview * Brandview;

}


Variable initialization


@implementation Enterhondaviewcontroller

-(ID) initWithFrame: (CGRect) frame

{

self = [super init];

if (self) {

Self.view.frame = frame;

[Self.view Setbackgroundcolor:[uicolor Whitecolor];


Uiimageview * background = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 0,1024, 768)];

[Self.view Addsubview:background];

[Background release];

[Background setimage:[uiimage imagenamed:@ "ALLAUTO_IMAGE_BG"];


UIButton * backbtn = [[UIButton alloc]initwithframe:cgrectmake (50, 18, 55,40)];

[Backbtn setimage:[uiimage imagenamed:@ "Home_button"] forstate:uicontrolstatenormal];

[Backbtn addtarget:self Action: @selector (Onback:) forcontrolevents:uicontroleventtouchupinside];

[Self.view ADDSUBVIEW:BACKBTN];

[BACKBTN release];


UILabel * Titlelabel = [[UILabel alloc] Initwithframe:cgrectmake (160, 25, 400, 35)];

Titlelabel.backgroundcolor = [Uicolor Clearcolor];

Titlelabel.textalignment = Nstextalignmentleft;

Titlelabel.font = [Uifont fontwithname:@ "Arial" size:30];

Titlelabel.textcolor = [Uicolor colorwithred:0.4 green:0.4 blue:0.4 alpha:1.0];

[Self.view Addsubview:titlelabel];

[Titlelabel release];

[Titlelabel settext:@ "into the broad book"];


Uiimageview * Lineview = [[Uiimageview alloc] Initwithframe:cgrectmake ((frame.size.width-658)/2,70, 658, 8)];

Lineview.image = [UIImage imagenamed:@ "ALLAUTO_CONFIG_TIMELINE_BG"];

[Self.view Addsubview:lineview];

[Lineview release];


UIView * Logoview = [[UIView alloc] Initwithframe:cgrectmake (780, 80, 240, 25)];

[Self.view Addsubview:logoview];

[Logoview release];


Imagelogo = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 6, 12, 13)];

[Logoview Addsubview:imagelogo];

[Imagelogo release];

Imagelogo.image = [UIImage imagenamed:@ "Allauto_corporation_button_select"];


Btn_corporate = [[UIButton alloc] Initwithframe:cgrectmake (13, 0, 100, 25)];

[Logoview Addsubview:btn_corporate];

Btn_corporate.tag = 1;

[Btn_corporate release];

[Btn_corporate addtarget:self Action: @selector (Onoptionclick:) forcontrolevents:uicontroleventtouchupinside];

[Btn_corporate settitle:@ "Corporate culture" forstate:uicontrolstatenormal];


Btn_brand = [[UIButton alloc] Initwithframe:cgrectmake (133, 0, 100, 25)];

[Logoview Addsubview:btn_brand];

Btn_brand.tag = 3;

[Btn_brand release];

[Btn_brand addtarget:self Action: @selector (Onoptionclick:) forcontrolevents:uicontroleventtouchupinside];

[Btn_brand settitle:@ "brand Story" forstate:uicontrolstatenormal];



Corporateview = [[Corporateview alloc] Initwithframe:cgrectmake (0, A, frame.size.width, frame.size.height-120)];

Brandview = [[Brandview alloc] Initwithframe:cgrectmake (0, A, frame.size.width, frame.size.height-110)];


[Btn_corporate Sendactionsforcontrolevents:uicontroleventtouchupinside];


[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (ongobrandpage:) name:notification_ Navigate_to_brand Object:nil];

}

return self;

}

Variable release

-(void) dealloc

{

[[Nsnotificationcenter defaultcenter]removeobserver:self];

[Brandview release];

[Corporateview release];

[Super Dealloc];

}

2 release of the property

Property declaration


@interface Ggdetailcell:uigridviewcell {

}

@property (nonatomic, retain) UILabel *labeltitle;

@end

Property class


-(ID) init {


if (self = [super init]) {


Self.frame = CGRectMake (0, 0, 66, 30.5);


{//Labeltitle

Self.labeltitle = [[[UILabel alloc] Initwithframe:cgrectmake (0, 7, +)] [autorelease];

[Self.labeltitle Setbackgroundcolor:[uicolor Clearcolor];

[Self.labeltitle Settextcolor:text_color_part_normal];

[Self.labeltitle Setfont:[uifont Systemfontofsize:12];

[Self.labeltitle setnumberoflines:0];

[Self.labeltitle Settextalignment:uitextalignmentcenter];


[Self addSubview:self.labelTitle];

}


}


return self;


}

Property deallocation
-(void) Dealloc {

Self.labeltitle = nil;

[Super Dealloc];

}

3 Release of the timer

Timer declaration:


@interface Brlandscapeview

nstimer* timer;

}
@end

Regular initialization:


-(void) myinit{

{//set Timer

Timer = [Nstimer scheduledtimerwithtimeinterval:1

Target:self

Selector: @selector (handletimer:)

Userinfo:nil

Repeats:yes];

}
Timer release: If the actual view in the declaration of initialization, the controller in the view before the release of the timer, otherwise due to the circular reference, and can not be released

@implementation Bookreadcontroller

-(void) dealloc

{

if (Landscape) {

[Landscape->timer invalidate];

}

Saferelease (Landscape);


[Super Dealloc];

}

@end


4 release of the notice


-(void) dealloc

{

[[Nsnotificationcenter defaultcenter]removeobserver:self];

[Super Dealloc];

}

5 release of the delegate
The assignment of the delegate property is generally self, although the declaration is assign, but when the associated View is released, it is released first delegate

Situation One


if (_loadingcontentview) {

_loadingcontentview.delegate = nil;

[_loadingcontentview Removefromsuperview];

}

Situation Two

Self.partGridView.uiGridViewDelegate = nil;

Self.partgridview = nil;


6 memory management for functions with return values

If a function requires a return value, this return value is declared and initialized within the function, but the lack is not immediately released, and the best way to handle it is as follows:


-(nsarray*) Gettemplatesbypath: (NSString *) path

{

Nserror * error = NIL;


nsarray* files = [FileManager Contentsofdirectoryatpath:path error:&error];


if (Error! = nil)

return nil;


nsmutablearray* resultarray = [Nsmutablearray new];


for (Nsinteger index=0; index < [files count]; index++)

{

nsstring* fileName = [Files Objectatindex:index];

nsstring* Exttype = [FileName pathextension];

if (NO = = [Exttype isequaltostring:@ "TPL"])

Continue

[Resultarray Addobject:filename];

}

return [Resultarray autorelease];

}


Extended

1 variables initialized, run out of the situation can be released immediately


Uiimageview * Lineview = [[Uiimageview alloc] Initwithframe:cgrectmake ((frame.size.width-658)/2,70, 658, 8)];

Lineview.image = [UIImage imagenamed:@ "ALLAUTO_CONFIG_TIMELINE_BG"];

[Self.view Addsubview:lineview];

[Lineview release];

2 when declaring, is the consideration of declaring a variable or a property.


In the first version of iOS, we declare both the attribute and the underlying instance variable for the output, at which point the attribute is a new mechanism for the OC language and requires that you declare the instance variable that corresponds to it, for example:

@interface Myviewcontroller:uiviewcontroller

{

UIButton *mybutton;

}

@property (nonatomic, retain) UIButton *mybutton;

@end

More recently, Apple has converted the default compiler from GCC to LLVM, which is no longer required to declare instance variables for a property.

If LLVM discovers a property that does not have a matching instance variable, it automatically creates an instance variable that begins with the underscore. Therefore, in this version, we no longer declare instance variables for the export output.

For example:

MyViewController.h file


@interface Myviewcontroller:uiviewcontroller

@property (nonatomic, retain) UIButton *mybutton;

@end

In the myviewcontroller.m file
The compiler will also automatically generate an instance variable _mybutton

The _mybutton instance variable can be used directly in the. m file, or through the property Self.mybutton. It's all the same.

Notice here that the Self.mybutton is actually called the Getter/setter method of the MyButton property

This is different from the use of midpoint in C + +, where points in C + + can directly access member variables (that is, instance variables)

For example, in OC there is the following code

. h file


@interface Myviewcontroller:uiviewcontroller

{

NSString *name;

}

@end

In the. m file
An expression such as self.name is wrong. Xcode will prompt you to use-> change it to Self->name.

Because the OC midpoint expression represents the calling method, the above code does not have the name method.

OC syntax Description of the point expression:

The point expression (.) It looks a bit like the access to the struct in C and the Java language Summary of object access, which is intended by OC designers.

If a point expression appears on the equals sign = left, the setter method for the property name is called. If the point expression appears on the right, the getter method for the property name is called. "

So the expression in OC is actually a shortcut to the setter and getter method of the calling object, for example:

Dealie.blah = Greeble is completely equivalent to [Dealie.blah setblah:greeble];


In the previous usage, declare the attribute with the instance variable that corresponds to it:


@interface Myviewcontroller:uiviewcontroller

{

UIButton *mybutton;

}

@property (nonatomic, retain) UIButton *mybutton;

@end

This approach is mostly used, and most of it is now in use, since many open source code is this way.
However, after the iOS5 update, Apple is recommended to use the following methods


@interface Myviewcontroller:uiviewcontroller

@property (nonatomic, retain) UIButton *mybutton;

@end

This is because the compiler automatically generates an instance variable _mybutton that starts with an underscore. You do not have to manually write instance variables yourself.

And there is no need to write @synthesize MyButton in the. m file; The Setter,getter method is automatically generated for you.

@synthesize's role is to have the compiler automatically generate setter and getter methods for you.

It also has the function of specifying an instance variable corresponding to the property,
For example @synthesize MyButton = xxx;

Then Self.mybutton is actually the instance variable xxx of the operation, not _mybutton.

In the actual project, we generally write this. m file

@synthesize MyButton;


Once this is written, the compiler automatically generates MyButton instance variables, along with the corresponding getter and setter methods.

Note: _mybutton This instance variable does not exist because the automatically generated instance variable is MyButton instead of _mybutton.

So now the @synthesize function is equivalent to specifying the instance variable,

If @synthesize MyButton is written in the. m file, then the generated instance variable is MyButton.

If you do not write @synthesize MyButton, then the generated instance variable is _mybutton.

So there is a slight difference from the previous usage.


Note: This is distinguished from the attributes added in the category, because only methods can be added to the category, and instance variables cannot be added.

It is often seen in iOS code to add attributes to a category, in which case variables are not generated automatically.

such as in

The Uiviewcontroller class is extended in the UINavigationController.h file


@interface Uiviewcontroller (Uinavigationcontrolleritem)

@property (Nonatomic,readonly,retain) Uinavigationitem *navigationitem;

@property (nonatomic) BOOL hidesbottombarwhenpushed;

@property (Nonatomic,readonly,retain) Uinavigationcontroller *navigationcontroller;

@end

The attributes added here do not automatically generate instance variables, and the attributes added here are actually added getter and setter methods.
Note that anonymous categories (anonymous extensions) can add instance variables, and non-anonymous categories cannot add instance variables, only methods, or properties (which are actually methods).


ARC

With respect to strong references, weak references, the use of arc can be viewed in the full guide to file Ios5arc.


iOS5 introduced arc is a lot of convenience, the arc of the rules is very simple:

As long as there is another variable pointing to the object, the object remains in memory. When the pointer points to a new value, or if the pointer no longer exists, the associated object is automatically freed.

This rule is applicable for instance variables, synthesize properties, and local variables.

When there is no arc before, you must call the

Dealloc method to free up memory, such as:


-(void) Dealloc {

[_mytableview release];

[Superdealloc];

}

If arc is used, the Dealloc method is no longer needed, and there is no need to worry about release issues anymore. The system will automatically manage the memory.

iOS Development app Value Dealloc What should I write?

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.