你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS開發之內存管理--dealloc該寫些什麼

IOS開發之內存管理--dealloc該寫些什麼

編輯:IOS開發綜合

在非ARC開發環境中,dealloc是類釋放前,清理內存的最後機會。到底那些變量和屬性該釋放呢,一些特殊的類(nstimer,observer)該怎麼釋放。需要注意的是不釋放會引起內存洩露,過度釋放也會引起內存洩露,接下來會慢慢展開:

1 變量的釋放

    變量聲明


@interface EnterHondaViewController : UIViewController{

    UIImageView * imageLogo;

    UIButton    * btn_Corporate;

    UIButton    * btn_Brand;

   
    CorporateView   * corporateview;

    BrandView       * brandview;

}

 


  變量初始化


@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:@"走進廣本"];

       
        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:@"企業文化" 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:@"品牌故事" forState:UIControlStateNormal];

       
       
        corporateview = [[CorporateView alloc] initWithFrame:CGRectMake(0, 110, frame.size.width, frame.size.height-120)];

        brandview = [[BrandView alloc] initWithFrame:CGRectMake(0, 110, 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;

}

 

   變量釋放
  

-(void)dealloc

{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

    [brandview release];

    [corporateview release];

    [super dealloc];

}

2 屬性的釋放

屬性聲明


@interface GGDetailCell : UIGridViewCell {

 

}

 

@property (nonatomic, retain) UILabel *labelTitle;

 

@end

 

屬性初始化


- (id)init {


    if (self = [super init]) {


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

       
        { // labelTitle

            self.labelTitle  = [[[UILabel alloc] initWithFrame:CGRectMake(0, 7, 66, 16)] 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;


}

屬性釋放
- (void)dealloc {

    self.labelTitle = nil;

    [super dealloc];

}

 

 

3 定時器的釋放

定時器聲明:


@interface BRLandscapeView

NSTimer* timer;

}
@end

定期初始化:


-(void) myInit{

    {//set timer

        timer = [NSTimer scheduledTimerWithTimeInterval: 1

                                                 target: self

                                               selector: @selector(handleTimer:)

                                               userInfo: nil

                                                repeats: YES];

}
定時器釋放:如果實在view中聲明初始化的,要在  controller中view釋放前先釋放定時器,否則由於循環引用,而釋放不掉

 

@implementation BookReadController

 

-(void)dealloc

{

    if (landscape) {

        [landscape->timer invalidate];

    }

    SafeRelease(landscape);

   
    [super dealloc];

}

@end

 

 


4 通知的釋放


-(void)dealloc

{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

    [super dealloc];

}

5 delegate的釋放
delegate屬性的賦值一般為self,雖然聲明時assign,但在相關的view釋放時,在之前先釋放掉delegate

情況一


if (_loadingContentView) {

        _loadingContentView.delegate = nil;

        [_loadingContentView removeFromSuperview];

    }

情況二 

self.partGridView.uiGridViewDelegate = nil;

    self.partGridView = nil;

 


6 有返回值的函數的內存管理

如果一個函數需要一個返回值,此返回值在函數內聲明和初始化,但缺不能立即釋放,最好的處理方式如下:


-(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];

}

 


擴展

1 變量初始化完,用完可立即釋放的情況


 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 聲明時,是聲明為變量還是屬性的考量。


在ios第一版中,我們為輸出口同時聲明了屬性和底層實例變量,那時,屬性是oc語言的一個新的機制,並且要求你必須聲明與之對應的實例變量,例如:

@interface MyViewController :UIViewController

{

UIButton *myButton;

}

@property (nonatomic, retain) UIButton *myButton;

@end

最近,蘋果將默認編譯器從GCC轉換為LLVM(low level virtual machine),從此不再需要為屬性聲明實例變量了。

如果LLVM發現一個沒有匹配實例變量的屬性,它將自動創建一個以下劃線開頭的實例變量。因此,在這個版本中,我們不再為輸出口聲明實例變量。

例如:

MyViewController.h文件


@interface MyViewController :UIViewController

@property (nonatomic, retain) UIButton *myButton;

@end

在MyViewController.m文件中
編譯器也會自動的生成一個實例變量_myButton

那麼在.m文件中可以直接的使用_myButton實例變量,也可以通過屬性self.myButton.都是一樣的。

注意這裡的self.myButton其實是調用的myButton屬性的getter/setter方法

這與c++中點的使用是有區別的,c++中的點可以直接訪問成員變量(也就是實例變量)

例如在oc中有如下代碼

.h文件


@interface MyViewController :UIViewController

{

NSString *name;

}

@end

.m文件中
self.name 這樣的表達式是錯誤的。xcode會提示你使用->,改成self->name就可以了。

因為oc中點表達式是表示調用方法,而上面的代碼中沒有name這個方法。

oc語法關於點表達式的說明:

"點表達式(.)看起來與C語言中的結構體訪問以及java語言匯總的對象訪問有點類似,其實這是oc的設計人員有意為之。

如果點表達式出現在等號 = 左邊,該屬性名稱的setter方法將被調用。如果點表達式出現在右邊,該屬性名稱的getter方法將被調用。"

所以在oc中點表達式其實就是調用對象的setter和getter方法的一種快捷方式, 例如:

dealie.blah = greeble 完全等價於 [dealie.blah setBlah:greeble];

 


以前的用法,聲明屬性跟與之對應的實例變量:


@interface MyViewController :UIViewController

{

UIButton *myButton;

}

@property (nonatomic, retain) UIButton *myButton;

@end

這種方法基本上使用最多,現在大部分也是在使用,因為很多開源的代碼都是這種方式。
但是ios5更新之後,蘋果是建議以以下的方式來使用


@interface MyViewController :UIViewController

@property (nonatomic, retain) UIButton *myButton;

@end

因為編譯器會自動為你生成以下劃線開頭的實例變量_myButton。不需要自己手動再去寫實例變量。

而且也不需要在.m文件中寫@synthesize myButton;也會自動為你生成setter,getter方法。

@synthesize的作用就是讓編譯器為你自動生成setter與getter方法。

它還有一個作用,可以指定與屬性對應的實例變量,
例如@synthesize myButton = xxx;

那麼self.myButton其實是操作的實例變量xxx,而不是_myButton了。

在實際的項目中,我們一般這麼寫.m文件

@synthesize myButton;


這樣寫了之後,那麼編譯器會自動生成myButton的實例變量,以及相應的getter和setter方法。

注意:_myButton這個實例變量是不存在的,因為自動生成的實例變量為myButton而不是_myButton。

所以現在@synthesize的作用就相當於指定實例變量,

如果.m文件中寫了@synthesize myButton;那麼生成的實例變量就是myButton。

如果沒寫@synthesize myButton;那麼生成的實例變量就是_myButton。

所以跟以前的用法還是有點細微的區別。

 


注意:這裡與類別中添加的屬性要區分開來,因為類別中只能添加方法,不能添加實例變量。

經常會在ios的代碼中看到在類別中添加屬性,這種情況下,是不會自動生成實例變量的。

比如在

UINavigationController.h文件中會對UIViewController類進行擴展


@interface UIViewController (UINavigationControllerItem)

@property(nonatomic,readonly,retain) UINavigationItem *navigationItem;

@property(nonatomic) BOOL hidesBottomBarWhenPushed;

@property(nonatomic,readonly,retain) UINavigationController *navigationController;

@end

這裡添加的屬性,不會自動生成實例變量,這裡添加的屬性其實是添加的getter與setter方法。
注意一點,匿名類別(匿名擴展)是可以添加實例變量的,非匿名類別是不能添加實例變量的,只能添加方法,或者屬性(其實也是方法)。

 


ARC

關於強引用,弱引用,arc的使用可以查看文件ios5arc完全指南。

 


ios5引進的arc確實方便了很多,ARC 的規則非常簡單:

只要還有一個變量指向對象,對象就會保持在內存中。當指針指向新值,或者指針不再存在時,相關聯的對象就會自動釋放。

這條規則對於實例變量、synthesize 屬性、本地變量都是適用的。

以前沒有arc的時候,必須調用

dealloc方法來釋放內存,比如:


- (void)dealloc {

    [_myTableView release];

    [superdealloc];

}

如果使用了ARC,那麼將不再需要dealloc方法了,也不需要再擔心release問題了。系統將自動的管理內存。

 


 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved