2012-08-06 49 views
0

我知道當我們使用自定義線程來創建對象並使用它們時。我在iOS應用程序中嘗試了下面的代碼,它沒有拋出任何錯誤。爲什麼?爲什麼不使用autorleasepool塊不會拋出錯誤?

-(void)Sample{ 

      NSLog(@"Sample"); 
      NSString *temp= @"asdfasdf"; 

      NSArray *tempAray = [NSArray arrayWithObject:temp]; 


      NSLog(@"Print it %@%s",temp,__FUNCTION__); 

} 

-(void)viewDidLoad{ 

      [super viewDidLoad]; 
      [NSThread detachNewThreadSelector:@selector(Sample) toTarget:self withObject:@"NSSstint"]; 
      // Do any additional setup after loading the view, typically from a nib. 
} 

編輯:

我明白的事實,我會泄漏內存,如果我已經通過自動釋放消息的對象。 我用下面的方法實現對樣品的方法調用: 即使是現在我沒有收到以下消息:

*** __NSAutoreleaseNoPool(): object 0x167ba4 autoreleased without a pool in place - just leaking *** 


-(void)Sample{ 

    NSLog(@"Sample"); 
    NSString *temp=[[NSString alloc] initWithFormat:@"Sample"]; 

    NSArray *tempAray = [NSArray arrayWithObject:temp]; 
    [tempAray retain]; 
    [tempAray autorelease]; 
    [temp autorelease]; 


    NSLog(@"Print it %@%s",temp,__FUNCTION__); 

} 

回答

1

,因爲它只給你消息記錄它不thow錯誤。如果您在自動釋放一個新的線程對象,而無需創建一個自動釋放池,你會得到噸的消息像

*** __NSAutoreleaseNoPool(): object 0x167ba4 autoreleased without a pool in place - just leaking *** 

但這一樣拋出NSExcpetion。此外,您可能從中得到的「它工作正常」的印象是錯誤的:您會泄漏內存,並且您的應用程序偶爾會在內存不足時崩潰。

+0

謝謝H2CO3。我更新了我的問題。你能否澄清爲什麼我沒有收到你所提到的信息? – Krishnan 2012-08-07 05:27:34

相關問題