NSConnection problems
NSConnection problems
- Subject: NSConnection problems
- From: email@hidden (mikevannorsdel)
- Date: Sat, 13 Oct 2001 23:46:06 -0600
I've ran into a couple of problems using NSConnection for inter-task
communications. The first problem is this:
I have an application which will be the server of the connection,
receiving and processing messages from a client task. However, the
server's main thread is usually too busy to respond to messages and
process them. So I wanted to have a secondary thread who's whole job
would be to accept the messages from the client and process them. I
only want this thread to receive the messages. So to implement this, I
created an NSConnection in the server object's init method:
- (id)init
{
self = [super init];
serverConnection = [[NSConnection alloc] init]; //instance var
[serverConnection setRootObject:self];
if([serverConnection registerName:@"myServer"] == NO)
NSLog(@"Could not register server connection");
[serverConnection runInNewThread];
return self;
}
This does indeed spawn a new thread for the NSConnection. But in actual
use, messages sent from the client are not always processed by the
secondary thread. Some times the main thread will handle the message,
and some times a third thread will be spawned and handle the message.
This is not what I want to happen. Is there a better way to set this up
so that incoming messages are only processed by the thread I choose?
And the second problem is with shared objects. Some of the messages
from the client contain NSObjects, either sent byref or bycopy. In one
instance, I want the client to send a pointer of an array to the server
where the server will populate the client's array. The method is
somewhat like this:
- (void)populateArray:(byref out NSMutableArray **)inArray
{
[*inArray addObject:someObject];
}
However, this doesn't work. The referenced array is never populated on
the client side. I could create the array on the server side and have
it as a return value, but the autolrelease pool limitations in the
NSConnection won't allow this. As far I can tell, an autorelease pool
must be created at each message on the server side, as there is no pool
to start with. So the alternate method won't quite work:
- (NSArray*)populateArray
{
NSAutoreleasePool * localPool = [[NSAutoreleasePool alloc] init];
NSMutableArray * outArray = [[NSMutableArray alloc] init];
[outArray addObject:someObject];
return [outArray autorelease];
}
As you can see, there's no where to release either the pool or the
array. And I have a leak. I'm not sure how to make this work either.
And the third problem is with object retaining by the NSConnection. If
I create an NSConnection which stays open during the life of the server
application, what happens to the shared objects? For instance:
- (oneway void)printString:(bycopy in NSString*)string
{
//some code here
}
The NSString object I assume is autoreleased by the NSConnection. But
when will it be released? Not until the NSConnection is closed or
released? Or at the end of the method's scope? Released at all?
Thanks for an insight on this.