Question

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. 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:

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

}
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top