drawing NOW in a callback, boom!
drawing NOW in a callback, boom!
- Subject: drawing NOW in a callback, boom!
- From: Paul Cezanne <email@hidden>
- Date: Tue, 4 Jun 2002 18:17:56 -0400
I'm redrawing a view from a callback in a thread and crashing. I'm not sure why I'm crashing. Let me set up the situation for you with some code snippets.
I start off with a naughty global to my controller:
void *mySelf;
in my awakeFromNib method I set this:
- (void)awakeFromNib
{
mySelf = self;
Right away I'm not too comfortable with this, but I need it, or at least I think I do. I have some other application specific globals that relate to the task at hand. (I talk about this more in #3 below.)
I have this routine in one of my .mm files (the same file as the controller). Notice that it calls a method of my controller.
void* CalTargetReplacement(void * parmBlock)
{
MyController *obj = (MyController *) mySelf;
[obj myDoPatchCallback : R green:G blue:B];
}
the method that it calls look like this:
- (void) myDoPatchCallback :(int)r green:(int)g blue:(int)b
{
printf("myDoPatchCallback %d %d %d\n", r, g, b);
[myTarget setPatch:r green:g blue:b];
[myTarget display];
usleep(10000);
}
I call display, not setNeedsDisplay, because when this callback returns the thread expects the screen to have the new color painted on it right then and there. (The sleep was the dead chicken I waved over it to get it to work, ...didn't help.) myTarget is a custom view which is pretty simple, here's the entire implementation.
- (void) drawRect:(NSRect) rect
{
NSRect bounds = [self bounds];
[[NSColor colorWithDeviceRed:myRed green:myGreen blue:myBlue alpha:1.0] set];
[NSBezierPath fillRect:bounds];
}
- (void) setPatch :(int)r green:(int)g blue:(int)b;
{
myRed = r/255.0;
myGreen = g/255.0;
myBlue = b/255.0;
}
So, what's wrong? :-)
I have a couple of theories and observations:
1) Commenting out the call to "display" cures the problem, which isn't too useful since I need to have the display update.
2) Sometimes, but not always, I see "leaking" messages about a NSDeviceColor not being returned since there is no pool.
3) I'm not at all sure about my use of threads and globals and the global of "self" makes me real squeamish. I'm using the global "self" because after reading all the posts on mamasam about methodForSelector it seemed to me the advice was to have C (or C++) wrapper so the API that needs a C callback gets it AND the (id, SEL) stuff doesn't get into you way. I was able to use methodForSelector and have a method called back successfully, I just couldn't access the parameter to it without crashing.
This newbie appreciates any advice. Thanks!
Paul
_______________________________________________
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.