Delayed -dealloc occurs on worker thread if object is "working". How?
Delayed -dealloc occurs on worker thread if object is "working". How?
- Subject: Delayed -dealloc occurs on worker thread if object is "working". How?
- From: Jerry Krinock <email@hidden>
- Date: Wed, 2 Jun 2010 10:39:52 -0700
I allocate an object Foo on the main thread. Then I spin off a secondary thread and give it a long task to do there, but immediately release the object, on the main thread. I expect that the -release invoke -dealloc immediately, on the main thread. To my amazement, the object is not deallocced until the task is finished, and to my further amazement, the dealloc method runs on the secondary thread.
Someone please enlighten me. This is Mac OS 10.6.3, Objective-C garbage collection = unsupported.
Jerry Krinock
#import <Cocoa/Cocoa.h>
@interface Foo : NSObject {
}
@end
@implementation Foo
- (void)sleep5 {
NSLog(@"Sleeping for 5 on secondary thread") ;
sleep(5) ;
}
- (void)dealloc {
[super dealloc] ;
NSLog(@"Did dealloc a Foo") ;
}
@end
int main(int argc, char *argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init] ;
Foo* foo = [[Foo alloc] init] ;
[NSThread detachNewThreadSelector:@selector(sleep5)
toTarget:foo
withObject:nil] ;
[foo release] ;
sleep(10) ;
NSLog(@"Terminating") ;
[pool release] ;
return 0 ;
}
RESULT:
10:23:00.879 LittleTool[83071:1103] Sleeping for 5 on secondary thread
10:23:05.884 LittleTool[83071:1103] Will dealloc a Foo
10:23:05.885 LittleTool[83071:1103] Did dealloc a Foo
10:23:10.879 LittleTool[83071:80f] Terminating
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden