Threads, safeness, and AutoreleasePools
Threads, safeness, and AutoreleasePools
- Subject: Threads, safeness, and AutoreleasePools
- From: Todd Heberlein <email@hidden>
- Date: Tue, 14 Aug 2001 19:59:05 -0700
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?
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?
- (void)runInThread
{
NSAutoreleasePool *subpool = [[NSAutoreleasePool alloc] init];
while (in_run_mode == YES) {
count++;
[count_field setIntValue: count];
}
[subpool release];
}
Thanks,
Todd