簡述OC中內存管理機制。


  1        簡述OC中內存管理機制。與retain配對使用的方法是dealloc還是release,為什么?需要與alloc配對使用的方法是dealloc還是release,為什么?readwrite,readonly,assign,retain,copy,nonatomic 、atomic、strong、weak屬性的作用?


 OC使用了一種叫做引用計數的機制來管理對象,如果對一個對象使用了alloc、[Mutable]copy,retain,那么你必須使用相應的realease或者autorelease。也可以理解為自己生成的對象,自己持有。非自己生成的對象,自己也能持有。不在需要自己持有的對象時釋放。非自己持有的對象無法釋放。生成並持有對象<alloc,new,copy,mutableCopy等>,持有對象<retain>,釋放對象<release>,廢棄對象<dealloc>。readwrite(默認):可讀可寫,表示既有getter方法,也有setter方法。readonly:表示只有getter方法,沒有setter方法。nonatomic:不考慮線程安全。atomic(默認):線程操作安全。strong(默認):ARC下和MRC下retain一樣,weak(ARC下):和(MRC下)assign類似,區別是當weak指向的內存釋放掉后自動置為nil,防止野指針。
unsafe_unretained聲明一個若引用,但不會自動置為nil,可能會出現野指針。
線程安全下的setter和getter方法:
-      (NSString *)value{
@synchronized(self){
return [[_value retain] autorelease];
}
}
-      (void)setValue:(NSString *)aValue{
@synchronized(self){
[aValue retain];
[_value release];
_value = aValue;
}
}

對於dealloc函數有兩種做法,

一個是直接將實例變量release掉:

-(void)dealloc

  [subject release];

[super dealloc];

另一種是將變量relsease 掉再將它指向nil;

-(void)dealloc

[subject release]

  subject=nil;

[super dealloc];

 

 

兩種方法結果是一致的,但是有些許的差別。

變量在被release掉后,系統將該內存標識為可用,nil只是起到重置指針的作用。

但是在object-c中給nil對象發送消息是,什么也不會發生,這樣在調試的時候,很難找到出錯的地方,所以

在調試階段最后用第一種,為了上線的時候用第二種,

可以通過宏定義

  1. #if DEBUG  
  2. #define MCRelease(x) [x release]  
  3. #else  
  4. #define MCRelease(x) [x release], x = nil  
  5. #endif

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

 

變量的釋放

 

    變量聲明

 

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

}

屬性的釋放

 

屬性聲明

 

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

}

 

 

定時器的釋放

 

定時器聲明:

 

@interface BRLandscapeView

NSTimer* timer;

}

@end

 

定期初始化:

 

-(void) myInit{

    {//set timer

        timer = [NSTimer scheduledTimerWithTimeInterval: 1

                                                 target: self

                                               selector: @selector(handleTimer:)

                                               userInfo: nil

                                                repeats: YES];

}

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

 

@implementation BookReadController

 

-(void)dealloc

{

    if (landscape) {

        [landscape->timer invalidate];

    }

    SafeRelease(landscape);

    

    [super dealloc];

}

@end

 

 

通知的釋放

 

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

 

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

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

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

}

 

 

擴展

 

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

 

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

 

聲明時,是聲明為變量還是屬性的考量。

 

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中點表達式其實就是調用對象的settergetter方法的一種快捷方式例如:

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;也會自動為你生成settergetter方法。

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

它還有一個作用,可以指定與屬性對應的實例變量,

例如@synthesize myButton = xxx;

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

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

@synthesize myButton;

這樣寫了之后,那么編譯器會自動生成myButton的實例變量,以及相應的gettersetter方法。

注意:_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

這里添加的屬性,不會自動生成實例變量,這里添加的屬性其實是添加的gettersetter方法。

注意一點,匿名類別(匿名擴展)是可以添加實例變量的,非匿名類別是不能添加實例變量的,只能添加方法,或者屬性(其實也是方法)。

 

ARC

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

 

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

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

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

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

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

- (void)dealloc {

    [_myTableView release];

    [superdealloc];

}

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

 

3 眾所周知,iOS開發的時候,使用ARC的話,dealloc函數是不需要實現的,寫了反而會出錯。

但有些特殊的情況,dealloc函數還是需要的。

比如,在畫面關閉的時候,需要把ViewController的某些資源釋放,

viewDidDissppear不一定合適,viewDidUnload一般情況下只在memory warning的時候才被調用。

不用ARC的情況下,我們自然會想到dealloc函數。

其實ARC環境下,也沒有把dealloc函數禁掉,還是可以使用的。只不過不需要調用[supper dealloc]了。

 

舉個例子,畫面上有UIWebView,它的delegate是該畫面的ViewController,在WebView載入完成后,需要做某些事情,比如,把indicator停掉之類的。

如果在WebView載入完成之前關閉畫面的話,畫面關閉后,ViewController也釋放了。但由於WebView正在載入頁面,而不會馬上被釋放,等到頁面載入完畢后,回調delegateViewController)中的方法,由於此時ViewController已經被釋放,所以會出錯。(message sent to deallocated instance

解決辦法是在dealloc中把WebViewdelegate釋放。

-(void)dealloc {

    self.webView.delegate = nil;

}

今天我給大家分享平時不太能注意到的一個內存釋放問題!很多人不是不知道,而是沒有注意到這塊!因為這里是大家很不容易發現的問題,今天有個好友在群里突然問這個問題,我遇到過好多新手問同樣的問題!今天我給大家分享一下自己的心得

Objective語言中最頭疼的事就是內存釋放,申明一個變量后記得一定要釋放這個變量,在我的博客中已經有一些這方面的文章,我們定義的全局變量都是在 - (void)dealloc 函數中釋放的;

里面繼承了一個[super dealloc]方法,

有些同學平時自己釋放內存都是寫在 [super dealloc]的后面,但是在Objective中不能這樣寫,所有的釋放都必須寫在 [super dealloc]的前面。

-------錯誤的寫法--------

- (void)dealloc

    [super dealloc];

    [XXX release];

}

-------正確的寫法--------

- (void)dealloc

{

    [XXX release];

    [super dealloc];

}

原因是:“你所創建的每個類都是從父類,根類繼承來的,有很多實例變量也會繼承過來,這部分變量有時候會在你的程序內使用,它們不會自動釋放內存,你需要調用父類的 dealloc方法來釋放,然而在此之前你需要先把自己所寫類中的變量內存先釋放掉,否則就會造成你本類中的內存積壓,造成泄漏”.不過在IOS6有了ARC后就不用手動去釋放了,也沒有此函數了!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM