我知道当我们使用自定义线程创建对象并使用它们时。我在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.
}
编辑:
我理解如果我已经将autorelease消息传递给对象,我将泄漏内存。 我在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__);
}
答案 0 :(得分:1)
它不会产生错误,因为它只会为您提供日志消息。如果您在新线程中自动释放对象而不创建自动释放池,您将收到大量消息,如
*** __NSAutoreleaseNoPool(): object 0x167ba4 autoreleased without a pool in place - just leaking ***
但这不与抛出NSExcpetion相同。此外,你可能从中得到的“它工作正常”的印象是错误的:你会泄漏内存,你的应用程序偶尔会因内存不足而崩溃。