CF equivalent of NSPortMessage?
CF equivalent of NSPortMessage?
- Subject: CF equivalent of NSPortMessage?
- From: "James Trankelson" <email@hidden>
- Date: Mon, 27 Oct 2008 16:41:11 +0100
I've been running a multithreaded application that listens for
messages from other threads within the same application. I've build a
number of applications that work in this way, and they've worked well.
However, I've recently been trying to write the CoreFoundation
equivalent of my existing mechanism, and haven't yet had any success.
I'm wondering if someone can help me see where I'm going wrong...
My (working) approach is as follows. Somewhere in my application, I
spawn a thread, which creates a port and listens on it. I create the
thread like so:
[NSThread detachNewThreadSelector:@selector(listener:) toTarget:self
withObject:nil];
With the thread method as follows:
-(void) listener {
...
port = (NSMachPort *)[NSMachPort port];
[port setDelegate:self];
[[NSRunLoop currentRunLoop] port forMode:NSDefaultRunLoopMode];
for(;;) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]];
}
I send a message like so:
NSPortMessage *message = [[NSPortMessage alloc]
initWithSendPort:np->port receivePort:np->port components:nil];
[np->port sendBeforeDate:[NSDate distantFuture] components:nil
from:nil reserved:0];
[message sendBeforeDate:[NSDate distantFuture]]);
When my thread gets this message, the "handlePortMessage" delegate
gets called, and everything is OK. Done.
Now for my attempt at a CoreFoundation version:
I create the thread in the same way, and do all of the CoreFoundation stuff:
[NSThread detachNewThreadSelector:@selector(listener:) toTarget:self
withObject:nil];
-(void) listener {
...
CFMessagePortRef local =
CFMessagePortCreateLocal(kCFAllocatorDefault, CFSTR("myport"),
myCallBack, &portContext, false);
CFRunLoopSourceRef source =
CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, local, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
CFRunLoopRun();
Once this is set up, I try to send a message to this port in the
following manner:
CFMessagePortRef remote = CFMessagePortCreateRemote(NULL, CFSTR("myport"));
char *message = "Hello!";
CFDataRef data, returnData = NULL;
data = CFDataCreate(NULL, message, strlen(message)+1);
CFMessagePortSendRequest(remote, 0, data, 1, 1, kCFRunLoopDefaultMode,
&returnData);
And it appears to work okay, because when I send a message like this,
my "myCallBack" method indeed gets invoked. However, I invariably get
a BAD_ACCESS at random points during the execution of the callback.
Usually not the same place.
Any ideas on what would cause this type of behavior? My loop appears
to be set up appropriately... Is there a better way to translate the
NSPortMessage version to use CoreFoundation functionality?
Thanks for any help!
-jt
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden