Re: Crashing when updating the User Interface from a detached NSThread
Re: Crashing when updating the User Interface from a detached NSThread
- Subject: Re: Crashing when updating the User Interface from a detached NSThread
- From: Erik Buck <email@hidden>
- Date: Fri, 3 Feb 2006 14:25:33 -0500
I was looking for something like that. However, it doesn't seem to
help my problem. I have this code in the method that is performed in
a new, detached thread:
if ([previewWindow isVisible]) {
[previewView performSelectorOnMainThread:@selector
(setImage:)
withObject:gridImage waitUntilDone:YES];
}
If the previewWindow is visible, I can go through at max a few frames
of the movie before my app locks up (it doesn't seem to be a genuine
What makes you think it is safe to call [previewWindow isVisible]
from any thread other than the main thread? As I recall, the AppKit
is explicitly NOT thread safe unless it is specifically documents
that it is thread safe.
How about
- (void)updatePreviewWithImage:(NSImage *)anImage
{
if([[previewView window] isVisible]) {
[previewView setImage:anImage];
}
}
Then call like so
[self performSelectorOnMainThread:@selector(updatePreviewWithImage)
withObject:gridImage waitUntilDone:YES];
However, you still may have a problem because the main thread is
undoubtedly going to send messages to anImage. NSImage is probably
not thread safe. You must not send any messages to gridImage while
the main thread might also be sending messages.
How about
[self performSelectorOnMainThread:@selector(updatePreviewWithImage)
withObject:[gridImage copy] waitUntilDone:YES];
[gridImage release];
In this case, the waitUntilDone:YES is absolutely essential!
Of course, there is the problem that you better not access
previewView or [previewView window] at all from the worker thread or
bad things may happen when the main thread is also accessing them.
I have the best idea of all! Don't use multi-threading this way. I
have almost certainly forgotten some potential problem even with two
iterations of the solution above. It is nearly impossible to get
multi-threading to work right with multiple shared data structures
due to the need for complex mutex locks and potential for deadlocks,
priority inversion, race conditions, etc. Do you even know all the
data structures that involved in refreshing an image view with a new
image ? How are you going to protect them all ?
I suggest that you generate new images in a different process running
via NSTask. Use a message queue, pipe, or distributed objects to
send the images to the main thread. Have the main thread's run-loop
handle input from the message queue, pipe, or distributed object just
like any other kind of input. You could even use a timer to handle
refresh if you want. This is simpler to code. This has no practical
performance difference. This protects all data structures in each
process from each other.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden