Re: self release
Re: self release
- Subject: Re: self release
- From: lbland <email@hidden>
- Date: Mon, 12 May 2003 18:35:36 -0400
On Monday, May 12, 2003, at 05:27 PM, Wolfgang Ante wrote:
>
> Regarding the point that a thread should release its resources when
>
> it dies...
>
>
>
> According to the documentation for NSThread's
>
> -detachThreadSelector:toTarget:withObject:, the thread actually
>
> retains aTarget for the duration of the thread. Therefore, if
>
> runInBackground spins off a thread (as it most likely does), then the
>
> object will be automagically released when the thread completes. No
>
> retains, no ambiguities.
>
>
*That* is the ultimate answer to my (real) question! Sorry for
>
confusing things by asking the wrong question in the beginning.
>
>
So I don't have to [self release] at all. I somehow knew Cocoa would
>
be that nice, because I never saw [self release] anywhere before.
if aTarget has retain count 1 and is not autoreleased then the
retainCount goes to 2 on calling, and 1 on exit. It does not
deallocate. If you call [aTarget autorelease] in the main thread then,
according to the docs at least, you risk colliding the call stacks,
same if you call [aTarget release] after detachThread...
I would call [aTarget release] as the first line in the method
corresponding to the selector you pass in.
Note this result:
2003-05-12 18:28:10.202 test[801] Doing Test
2003-05-12 18:28:10.202 test[801] 1 [self retainCount]: 1
2003-05-12 18:28:10.204 test[801] 2 [self retainCount]: 2
2003-05-12 18:28:20.204 test[801] 3 [self retainCount]: 1
for this code:
- (void)test111:(id)sender
{
NSLog(@"2 [self retainCount]: %d\n", [self retainCount]);
[NSThread exit];
}
- (void)doTest:(id)sender
{
NSLog(@"Doing Test");
NSLog(@"1 [self retainCount]: %d\n", [self retainCount]);
[NSThread detachNewThreadSelector:@selector(test111:) toTarget:self
withObject:nil];
sleep(10);
NSLog(@"3 [self retainCount]: %d\n", [self retainCount]);
all IMHO.
There are other issues like stack corruption and doc ambiguity.
-lance
Lance Bland
mailto:email@hidden
VVI
888-VVI-PLOT
http://www.vvi.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.