簡體   English   中英

為什么不使用autorleasepool塊不會引發錯誤?

[英]Why not using autorleasepool block does not throw error?

我知道當我們使用自定義線程創建對象並使用它們時。 我在iOS應用程序中嘗試了以下代碼,但未引發任何錯誤。 為什么?

-(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.
}

編輯:

我了解以下事實:如果我已將自動釋放消息傳遞給對象,則將泄漏內存。 我將以下方法實現用於Sample方法調用:即使現在我也沒有收到以下消息:

*** __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__);

}

它不會發出錯誤,因為它只給您記錄消息。 如果您在新線程中自動釋放對象而不創建自動釋放池,則會收到大量消息,例如

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

但是,這是一樣拋出NSExcpetion。 同樣,您從中獲得的“效果很好”的印象是錯誤的:您將泄漏內存,並且由於內存不足,您的應用有時會崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM