Re: NSMessagePort crashes
Re: NSMessagePort crashes
- Subject: Re: NSMessagePort crashes
- From: Gwynne <email@hidden>
- Date: Sun, 23 Jan 2005 13:30:02 -0500
On Jan 23, 2005, at 11:15 AM, Stephan Lichtenauer wrote:
I have written a small test program to use
NSMessagePorts/NSPortMessages but at best the message is not received
and in the worst case the program crashes with a EXC_BAD_ACCESS.
Below is my code, when I initialize the message as it can be seen with
the distantPort, the app crashes, when I init. it with a send port of
nil it does not crash but the message is not received nevertheless
(push is attached to a button in the IB)... I looked everywhere on the
net and in my "Core Mac OS X and Unix Programming" and Cocoa books and
could not find much example code that would help, all the code I found
looked more or less what I did...
I suggest taking a look at the ThreadMessage code by Dustin Mierau,
which can be found at <http://www.blackholemedia.com/code/>. A quick
summary of what his code does differently than yours:
- Use [[NSPort port] retain] instead of NSMessagePort. NSPort creates
an NSMachPort by default, which will work better for OS X-local
messaging. I'm not sure if NSMessagePort by itself works at all.
- Use [localPort scheduleInRunLoop:forMode:] I know the headers say not
to do that, but he does it and it works.
- Use localPort as the send port and don't use any receive port. It's a
one-way message; the receive port is for replies.
- Don't use nil for the components. Send something, even if you just
put [NSArray arrayWithObject:[NSNull null]].
- Don't send before [NSDate distantPast]. Send before [NSDate
distantFuture]. You're basically telling it to fail instantly. The
timeout in that parameter only waits for a lock on the port's message
queue to become available. It won't give you a timeout in communication
with another thread.
- Finally, I suggest using a separate thread to test this. I doubt
NSPort semantics are well-defined for intra-thread messaging, and
they're certainly not necessary for it. Again, see Dustin's code.
Remember that a port you schedule on a given thread RECEIVES messages
from other threads that SEND to the same port. This can get a little
confusing if you're not careful. I got the semantics mixed up many
times.
A somewhat more complicated example of inter-thread messaging (two-way)
can be found in NDRunLoopMessenger, which you can get at
<http://homepage.mac.com/nathan_day/pages/source.html>.
Both ThreadMessage and NDRunLoopMessenger are light-weight forms of the
much more complicated Distributed Objects architecture, which I found
to be severely overdesigned and much too complex for my purposes. I
definitely recommend either one of these, or your own implementation,
depending on your needs.
-- Gwynne, key to the Code
Email: email@hidden
Website: http://musicimage.plasticchicken.com/
"This whole world is an asylum for the incurable."
@implementation Test
-(id)init {
[super init];
localPort = [[NSMessagePort alloc] init];
[localPort setDelegate:self]; // do I need this?
distantPort = [[NSMessagePort alloc] init];
[[NSRunLoop currentRunLoop] addPort:localPort
forMode:NSDefaultRunLoopMode];
return self;
}
- (void)handlePortMessage:(NSPortMessage *)portMessage {
NSLog(@"Msg received");
}
-(IBAction)push:(id)sender {
NSPortMessage *msg;
msg = [[NSPortMessage alloc] initWithSendPort:distantPort
receivePort:localPort
components:nil];
[msg setMsgid:10];
[msg sendBeforeDate:[NSDate distantPast]];
}
@end
Anybody any idea?
Thanks a lot!
Stephan
PS I know there are memory leaks but I wanted to keep the code as
simple as possible...
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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