Multi-leaf and standalone pools work together in Cocoa?

I would like to send the object back to the main thread from the workflow. However, do automatic release pools work between threads? Something is wrong with the following code:

-(void)mainThreadReceiveResult:(id)response { [response retain]; /* Do some stuff with response */ [response release]; } -(void)workerThreadDoWork { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; response * response = [[[response alloc] init] autorelease]; response->someData = [self getSomeData]; [delegate performSelectorOnMainThread:@selector(receiveResult:) withObject:response waitUntilDone:NO]; [pool release]; } 

Everything seems to be fine. However, is it possible for the worker thread to reach the [pool release] before the main thread can save it?

+6
multithreading objective-c cocoa
source share
2 answers

Your code should not performSelectorOnMainThread: : performSelectorOnMainThread: retains its arguments until the selector finishes working, so your hold / release pair will be redundant.

See the documentation :

This method saves the receiver and the arg parameter until a selector is executed.

Also: you should probably [pool drain] instead of [pool release] .

+7
source share

This may answer your question.

Here is what he did to solve the problem. An explanation is given in this link.

 - (void)runSomethingThatWillFail:(NSError **)error { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/BOGUS" error:error]; [*error retain]; [pool release]; [*error autorelease]; } 
0
source share

All Articles