2009-11-07 74 views
1

我已經在其中我已經指定泄漏線的代碼的和平。由於我是iPhone開發的新手,我無法理解這條線究竟有什麼問題。請看看那條線並告訴我。這條泄漏線背後的原因是什麼?

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
    //take away 
    //NSURL *url1 = [[NSURL alloc] initWithString:@"http://url/Service.asmx/takeAwayList"]; 
    NSURL *url1 = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@/Service.asmx/takeAwayList",serviceURL]]; 

    NSMutableURLRequest* request1=[NSMutableURLRequest requestWithURL:url1]; 
    [request1 setHTTPMethod:@"POST"]; 
    [request1 setTimeoutInterval:10]; 

    //*****the leaky line***********************/// 
    NSData *data2=[[NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil] autorelease]; 

    if(data2 == nil) 
    { 
     UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"The network is not available.\n Please check the Internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 

    } 
    else 
    { 

     NSXMLParser *xmlParser1 = [[NSXMLParser alloc] initWithData:data2]; 

     //Initialize the delegate. 
     TakeAwayParser *takeAwayParser = [[TakeAwayParser alloc] initTakeAwayParser]; 

     //Set delegate 
     [xmlParser1 setDelegate:takeAwayParser]; 

     //Start parsing the XML file. 
     @try { 
      BOOL success = [xmlParser1 parse]; 
      if(success) 
       NSLog(@"No Errors"); 
      else 
       NSLog(@"Error Error Error!!!"); 
     } 
     @catch (NSException * e) { 
      NSLog(@"Exception in parsing %@ %@",[e name], [e reason]); 
     } 
     [takeAwayParser release]; 
     [xmlParser1 release]; 
    } 

    //[request1 release]; 
// [response1 release]; 
// 
    [url1 release]; 
// [data2 release]; 
    //new arrivals 
    //[data2 release]; 
    [pool release]; 

回答

2

一個保留計數。在與蘋果工程師一起努力尋找漏洞之後,他終於向NSURLConnection背後的主要Apple開發團隊詢問。他們基本上說,有一個內部緩存在NSURLConnection中根本不可清除,這是一個已知問題。

所以我開始着手尋找替代品。我發現了ASIHTTPConnection(下面的鏈接),它起作用於CFNetwork。它被設計成爲NSURLConnection的一個直接替代品,再加上一堆其他真棒好東西,比如下載到磁盤而不是內存,下載恢復,進度條回調等。

我已經在我所有的項目中使用過它,從來沒有任何問題或投訴。一個,回答你的問題,這是我如何擺脫這些內存泄漏。

http://allseeing-i.com/ASIHTTPRequest/

+0

這是愚蠢的,但我仍處於學習階段,所以你可以指定我應該從你重定向的頁面取什麼,我應該在我的代碼中替換什麼? – harshalb 2009-11-07 06:23:53

+1

我覺得這個小代碼也有效。對於現在的手段是不是給我的網址任何進一步的泄漏把這個代碼後:NSMutableURLRequest * request1 = [NSMutableURLRequest requestWithURL:URL1的CachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0] \t [request1 setHTTPMethod:@「POST」]; \t // [request1 setTimeoutInterval:10]; \t [[NSURLCache sharedURLCache] setMemoryCapacity:0]; \t [[NSURLCache sharedURLCache] setDiskCapacity:0]; – harshalb 2009-11-07 07:23:18

+0

這真的很古老,但是hib在這裏有什麼功能。對不起,復活一個死去的線程;) – gabaum10 2010-11-19 20:53:21

1

這行不漏,你甚至不應該自動釋放它。

請你幫個忙,並在蘋果的開發者文檔,註釋的版本在你的代碼並不好兆頭讀取內存管理指南。

編輯:槽糕我收回這句話你的代碼,除了這一行完全沒問題。你確定它在泄漏嗎?它返回一個對象爲0的保留計數,所以你自動釋放應該引起麻煩,因爲它已經有我在大項目有問題,與此也爲0

+0

那麼爲什麼儀器每次都要把我帶到那條線上呢? – harshalb 2009-11-07 06:06:52

+0

如果我將刪除自動釋放,那麼儀器正在引導我。是我在某處看過的東西,那些樂器有時會出現假陽性或假泄漏? – harshalb 2009-11-07 06:12:32