2011-10-08 211 views
2

我試圖在這裏建立RSS閱讀器,問題是,當用戶完成閱讀ARTICAL並按回的dealloc不叫我的retainCount正在增加?

我得到了retainCount 6 &一些時間7!

我有很多定製的面板

時後退按鈕按下的POP操作的觀點,並沒有所謂的dealloc的?!

.h文件中:

@interface ArticalViewController : UIViewController<UIWebViewDelegate,UIScrollViewDelegate,UIActionSheetDelegate,ArticalBottomPanelDelegate,ArticalContentFetcherDelegate> { 

    UIWebView * description_; 
    UIActivityIndicatorView * ind_; 
    ArticalModel * artical_; 
    NSString * content; 
    UIButton * faceBookShareBtn_; 

    UIBarButtonItem * btnSharePanel_; 


    CustomTopToolBar * topToolbar_; 

    ArticalBottomPanel* articalBottomPanel_; 
    MovingSharePanel * movingSharePanel_; 

    int fontSize; 
    BOOL favoStatus; 
    ArticalContentFetcher *datafetcher_;  

} 


@property (nonatomic,retain) IBOutlet UIWebView * description; 
@property (nonatomic,retain) IBOutlet UIActivityIndicatorView * ind; 
@property (nonatomic,retain) ArticalModel * artical; 
@property (nonatomic,retain) IBOutlet UIButton * faceBookShareBtn; 


@property (nonatomic,retain) IBOutlet CustomTopToolBar * topToolbar; 
@property (nonatomic , retain) IBOutlet ArticalBottomPanel * articalBottomPanel; 
@property (nonatomic , retain) IBOutlet MovingSharePanel * movingSharePanel; 

@property (nonatomic , retain) ArticalContentFetcher *datafetcher; 




-(void) loadArtical:(ArticalModel *)artical; 
- (void) loadArticalContentFromInternet; 
-(void) changeFavoriteBtnIcon:(BOOL) status; 
-(void)backBtnPressed:(id) sender; 

-(IBAction)openPostBtnPressed:(id)sender; 

@end 

.m文件:

#import "ArticalViewController.h" 


    @implementation ArticalViewController 


    @synthesize description=description_; 
    @synthesize artical=artical_; 
    @synthesize ind=ind_; 

    @synthesize faceBookShareBtn=faceBookShareBtn_; 

    @synthesize topToolbar=topToolbar_; 
    @synthesize articalBottomPanel=articalBottomPanel_; 
    @synthesize movingSharePanel=movingSharePanel_; 
    @synthesize datafetcher=datafetcher_; 

    - (void)dealloc 
    { 
     NSLog(@"ArticalViewController : dealloc"); 
     [description_ release]; 
     [ind_ release]; 
     [artical_ release]; 
     [content release]; 
     [faceBookShareBtn_ release]; 

     [btnSharePanel_ release]; 


     [topToolbar_ release]; 

     [articalBottomPanel_ release]; 
     [movingSharePanel_ release]; 
     [datafetcher_ release]; 

     [super dealloc]; 
    } 

    #pragma mark - 
    #pragma mark - View lifecycle 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     fontSize=100; 


     [self.articalBottomPanel setDelegate:self]; 
     [self.movingSharePanel setArtical:self.artical]; 
     [self.movingSharePanel setParentView:self]; 


     [self.topToolbar.rightButon setFrame:CGRectMake(260 , 3, 50, 30)]; 
     [self.topToolbar.rightButon addTarget:self action:@selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [self.topToolbar.rightButon setImage:[UIImage imageNamed:@"back-button"] forState:UIControlStateNormal]; 

     [self.topToolbar.leftButon setFrame:CGRectMake(10 , 3, 50, 30)]; 
     [self.topToolbar.leftButon setImage:[UIImage imageNamed:@"button-toolbar-post-link"] forState:UIControlStateNormal]; 
     [self.topToolbar.leftButon addTarget:self action:@selector(openPostBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; 


     self.topToolbar.label.text =self.artical.category; 
     // [label release]; 

     //Navigation Buttons 
     self.navigationItem.hidesBackButton=YES; 

     //Check if artical is favorited or not 
     [self changeFavoriteBtnIcon:[Favorites chechArtical:self.artical]]; 



     [self.description setBackgroundColor:[UIColor clearColor]]; 
     [self.description setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"1px-post-views-background"]]]; 

     if([[self.artical content] length] >1){ 
      NSLog(@"[[self.artical content] length] >1"); 
      [self loadArtical:self.artical]; 
     }else { 
      NSLog(@"else"); 
      self.datafetcher=[[ArticalContentFetcher alloc ]init ]; 
      [self.datafetcher setArticalContentFetcherDelegate:self]; 
      [NSThread detachNewThreadSelector:@selector(loadArticalContentFromInternet) toTarget:self withObject:nil]; 

     } 




    } 
    #pragma mark - 
    #pragma mark ArticalConnectionFeed Delegate Methods 
    -(void) articalContentConnectionDoneWithArtical:(ArticalModel *)artical { 
     NSLog(@"articalContentConnectionDoneWithArtical"); 
     [self loadArtical:artical]; 
    } 
    -(void) articalContentConnectionFailed{ 
     NSLog(@"articalContentConnectionFailed"); 

    } 
    #pragma mark - 
    #pragma mark ArticalBottomPanelDelegate Delegate Methods 

    -(void) openPanelFired{ 
     NSLog(@"openPanelFired"); 
     [self.movingSharePanel movePanel]; 
     // [self.articalBottomPanel.btnOpenSharePanel setHidden:YES]; 
    } 

    -(void) fontBtnFired:(int)font{ 
     // NSLog(@"fontBtnFired : %d",font); 
     if(font==1){ 
      [self.description stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '90%'"]; 
     }else { 
      [self.description stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '-10%'"]; 
     } 
    } 
    -(void) favoBtnFired { 
     NSLog(@"favoBtnFired"); 
     favoStatus=[Favorites processArtical:self.artical]; 
     [self changeFavoriteBtnIcon:favoStatus]; 
    } 

    -(void) changeFavoriteBtnIcon:(BOOL) status{ 
     if (status){ 
      [self.articalBottomPanel.btnFavo setImage: [UIImage imageNamed:@"active-star.png"] forState:UIControlStateNormal]; 

     }else { 
      [self.articalBottomPanel.btnFavo setImage: [UIImage imageNamed:@"star.png"] forState:UIControlStateNormal]; 
     } 
    } 
    #pragma mark - 
    #pragma mark UIWebView Delegate Methods 
    -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType { 
     if([InternetConnection getInternetStatus]){ 
      if (inType == UIWebViewNavigationTypeLinkClicked) { 
       [[UIApplication sharedApplication] openURL:[inRequest URL]]; 
       return NO; 
      } 
      return YES; 
     }else { 
      [InternetConnection ShowNoInternetAlert]; 
      return NO; 
     } 


    } 

    - (void)webViewDidFinishLoad:(UIWebView *)webView { 
     [self.ind stopAnimating]; 
    } 


    #pragma mark - 
    #pragma mark Class Methods 
    -(void) loadArtical:(ArticalModel *)artical{ 
     NSLog(@"loadArtical"); 
     [self.artical setContent:[artical content]]; 
     [self.artical setCategory:[artical category]]; 


     NSString * style=[[NSString alloc ] initWithFormat:@"<style> #offline img{display:none;} .wrap{text-align:right;line-height:22px; direction:rtl;} .title{font-size:20px;margin-bottom:5px;} .date{font-size:13px;} .cat{font-size:13px;} </style>"]; 

     if([InternetConnection getInternetStatus]) 
      NSLog(@"[InternetConnection getInternetStatus] : true"); 
     content=[[NSString alloc] initWithFormat:@"%@<div class='wrap' ><div class='title'>%@</div><div class='date'>%@</div><div class='cat'>%@</div>%@</div>",style,[self.artical title],[DateProcessor getInternetDateAndTimeForArticals:self.artical.pubDate],[self.artical category],[self.artical content] ]; 
     [ self.description loadHTMLString:content baseURL:[NSURL URLWithString:@""]]; 

     [style release]; 
    } 

    - (void) loadArticalContentFromInternet{ 
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
     [self.datafetcher loadArticlContentWithID:self.artical.ID]; 
     [[NSRunLoop currentRunLoop] run ]; 
     [pool release]; 


    } 
    -(void)backBtnPressed:(id) sender{ 
      NSLog(@"backBtnPressed"); 

     [self.navigationController popViewControllerAnimated:YES]; 


    } 

    #pragma mark - 
    #pragma mark IBActions  


    -(IBAction)openPostBtnPressed:(id)sender{ 
     if([InternetConnection getInternetStatus]){ 
      [InternetConnection openExternalUrl:self.artical.link]; 
     }else{ 
      [InternetConnection ShowNoInternetAlert]; 
     } 


    } 

    @end 

增加:

當我調用ARTICAL我用

avc=[[ArticalViewController alloc]initWithNibName:@"ArticalViewController" bundle:nil]; 
    avc.artical=[self.feeds objectAtIndex:indexPath.row]; 
    [self.navCon pushViewController:avc animated:YES]; 

    [avc release]; 
+0

看看這裏http://stackoverflow.com/questions/577635/iphone-when-is-dealloc-for-a-viewcontroller-called – Mat

+0

我看了之前發送這個Q,但我問的是我的情況,什麼是我的代碼錯了!? – Yahia

回答

41

我發現追蹤保留計數和丟失保留 - 釋放對的最佳方法是使用工具。 Hit Profile(Cmd⌘ + I)並選擇泄漏模板。即使泄漏不會自動發現,保留更改也會被記錄下來,以便您可以手動追蹤其他保留。要做到這一點,請在找到您的班級名稱對象摘要當選擇分配文書。如果你找不到它,這意味着所有的實例都被釋放。否則點擊時,您選擇的類名出現的箭頭: Select the class name and click on the arrow to view living instances 你會看到你的類的所有生活實例: Select the instance and click on the arrow to view its retains/releases

如果假定有應該已經釋放某些情況下,選擇一個,然後點擊在對象地址旁邊出現的箭頭上。現在你應該看到任何保留或釋放被調用該對象與已在執行此操作方法的名稱上: Double click to see what line of code invoked this retain/release

RefCt欄顯示retainCount被調用的動作後,當你在任何雙擊保留/釋放儀器會向您顯示執行此操作的代碼行: Responsible lines are highlighted

正如您所看到的,對象被添加到數組中並且永遠不會從中刪除。

根據我的經驗,這是找到內存泄漏的最快和最簡單的方法,即儀器不會自動檢測到泄漏。我認爲另一個良好的做法是看#活動對象摘要確保生存實例的數量完全符合您的預期。

0

NSThread detachNewThreadSelector:toTarget:withObject:保留其圓盾噸,在這種情況下是self。此外,自我是這裏幾件事的代表,通常你不想保留代表,所以如果你寫了這些協議,檢查你沒有這樣做。

+0

你的權利,我檢查了我的代表,現在我有retainCount 2,但我調用[self.navigationController popViewControllerAnimated:YES]後; ,保留數是4! – Yahia

+0

我加了[self retain]; before [self.navigationController popViewControllerAnimated:YES];和dealloc正確調用,但它是真的調用[self release]; !?這是解決方案嗎?! – Yahia

+0

彈出時的保留計數可能會有所不同,具體取決於您的loadArticalContentFromInternet方法是否在另一個線程上完成。你應該在彈出之前取消這個線程。無情地調用釋放是危險的。找出你被保留的地方,並適當地釋放。 – mbehan

9

您沒有理由關心保留計數是多少。只要確保你平衡你的保留和發佈。

5

看起來很奇怪,retainCount根本沒有用於計算您的保留。由於其他原因保留的東西不是你撥打[myObject retain][[MyClass alloc] init]等。

最好忽略retainCount並學習memory management rulesretainCount會讓你更加困惑。如果您僅開發iOS 5,最好忘記內存管理並使用ARC