Re: Threads, safeness, and AutoreleasePools
Re: Threads, safeness, and AutoreleasePools
- Subject: Re: Threads, safeness, and AutoreleasePools
- From: Raphael Sebbe <email@hidden>
- Date: Wed, 15 Aug 2001 09:03:42 +0200
On Wednesday, August 15, 2001, at 04:59 AM, Todd Heberlein wrote:
>
I have been experimenting with NSThreads, and I had a couple
>
questions. At
>
the bottom is a sample method I am testing. It is executed in a new
>
thread
>
with the detachNewThread:Selector:toTarget:withObject: method.
>
>
First, does Apple discourage us from sending a message to an AppKit
>
object
>
from a separate thread? For example, in the code below I send the
>
setIntvalue to an NSTextField object. Is this OK? Are there other
>
steps I
>
should do?
This is not OK. Only the main thread can access GUI objects.
You have a number of solutions to do that : DO (see NSConnection doc),
NSPort and NSPortMessage, AppleEvents, pipes...
>
>
The display mostly works, but sometimes the text gets a little messed
>
up.
>
>
Second, I had to create an AutoreleasePool to prevent the program
>
complaining about a leak when I send the setInt message. I assume it
>
allocates and releases objects within the method.
>
>
Is it common to create an AutoreleasePool for a new thread?
>
Yes, it is required. A lot of methods (and not only yours) do make use
of autoreleased objects. Note that logs will tell you objects have been
autoreleased with no pool in place.
>
- (void)runInThread
>
{
>
NSAutoreleasePool *subpool = [[NSAutoreleasePool alloc] init];
>
>
while (in_run_mode == YES) {
>
count++;
>
[count_field setIntValue: count];
>
}
>
[subpool release];
>
}
>
>
Thanks,
>
>
Todd
>
_______________________________________________
>
cocoa-dev mailing list
>
email@hidden
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev