简体   繁体   English

为什么不使用autorleasepool块不会引发错误?

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

I know that when we use a custom thread to create objects and use them. 我知道当我们使用自定义线程创建对象并使用它们时。 I tried the below code in an iOS application , it did not throw any error. 我在iOS应用程序中尝试了以下代码,但未引发任何错误。 Why? 为什么?

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

EDIT: 编辑:

I understand the fact that I will be leaking the memory if I have passed autorelease message to the objects. 我了解以下事实:如果我已将自动释放消息传递给对象,则将泄漏内存。 I used the below method implementation for Sample method call: Even now I did not receive the below message: 我将以下方法实现用于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__);

}

It doesn't thow an error because it only gives you logging messages. 它不会发出错误,因为它只给您记录消息。 If you autorelease objects in a new thread without creating an autorelease pool, you'll got tons of messages like 如果您在新线程中自动释放对象而不创建自动释放池,则会收到大量消息,例如

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

But this is not the same as throwing an NSExcpetion. 但是,这是一样抛出NSExcpetion。 Also, the "it works fine" impression you might get from this is wrong: you'll leak memory, and your app will occasionally crash upon low memory. 同样,您从中获得的“效果很好”的印象是错误的:您将泄漏内存,并且由于内存不足,您的应用有时会崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM