Re: When to release an NSThread instance
Re: When to release an NSThread instance
- Subject: Re: When to release an NSThread instance
- From: Roland King <email@hidden>
- Date: Sat, 07 Mar 2009 11:08:50 +0800
if you don't need to reference the thread again you can just release
it (and the target object used in initWithTarget:selector:object) as
soon as you've called -(void)start on it. You are correct that the
thread will exit when the method you are calling finishes and you
don't need to call exit. When an NSThread is started it retains its
target object and argument and the thread itself is retained
presumably by some subsystem which manages threads. When the task is
complete the target object and argument are released and the NSThread
is released automatically too. So
MyTargetObject *target = [ [ MyTargetObject alloc ] init ];
MyArgObject *arg = [ [ MyArgObject alloc ] init ];
NSThread *t = [ [ NSThread alloc ] initWithTarget:target
selector:@selector( someMethod: ) object:arg ];
[ t start ];
// in any order you like
[ target release ];
[ arg release ];
[ t release ];
should work fine.
On Mar 7, 2009, at 7:55 AM, Stuart Malin wrote:
I have a main thread method that allocates an NSThread object and
inits with initWithTarget:selector:object. I then invoke the NSTask
instance with -start.
The specified selector is invoked, and there performs a background
task that takes some time. When it is done, that method invokes a
callback using
performSelectorOnMainThread:withObject:waitUntilDone. That callback
selector updates the applications U/I to reflect the outcome of the
background task.
After invoking the callback, the background task method cleans up,
releases its autorelease pool, and then control reaches the end of
the method. I am under the impression that having the method reach
the end is sufficient to have the thread end. Is that correct? or do
I need to invoke the thread instance with -exit ?
Big question: how do I release the memory associated with the
allocated task instance? I have two notions:
1) have the main thread callback autorelease the thread instance
2) have the thread autorelease itself at the end of the background
task method after it invokes the callback (which is invokes
with ...waitUntilDone:YES).
I know I am treading in dangerous waters with threads... but the
background activity can take many seconds, and I want the U/I
responsive while the background activity is being performed.
_______________________________________________
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
_______________________________________________
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