Re: one NSPipe, multiple NSTasks?
Re: one NSPipe, multiple NSTasks?
- Subject: Re: one NSPipe, multiple NSTasks?
- From: beamsplitter <email@hidden>
- Date: Mon, 24 Mar 2003 19:36:43 -0500
On Monday, March 24, 2003, at 04:57 AM, Marcel Weiher wrote:
On Monday, March 24, 2003, at 05:41 Uhr, beamsplitter wrote:
I'd like to use one NSPipe instance for multiple successive NSTasks.
Why?
Because it's cleaner and easier.
I've tried this but can't get it to work:
NSPipe *myPipe = [[NSPipe alloc] init];
NSTask *myTask = [[[NSTask alloc] init] autorelease];
NSTask *myNextTask = [[[NSTask alloc] init] autorelease];
[myTask setStandardOutput: myPipe];
[myTask setStandardError: myPipe];
[[NSNotificationCenter defaultCenter] addObserver: self selector:
@selector(getPipeOutput:) name: NSFileHandleReadCompletionNotification
object: [myPipe fileHandleForReading]];
[[myPipe fileHandleForReading] readInBackgroundAndNotify];
[myTask launchTask];
// ...
// task has terminated
[[NSNotificationCenter defaultCenter] removeObserver: self name:
NSFileHandleReadCompletionNotification object: [myPipe
fileHandleForReading]];
// ...
[myNextTask setStandardOutput: myPipe];
[myNextTask setStandardError: myPipe];
[[NSNotificationCenter defaultCenter] addObserver: self selector:
@selector(getPipeOutput:) name: NSFileHandleReadCompletionNotification
object: [myPipe fileHandleForReading]];
[[myPipe fileHandleForReading] readInBackgroundAndNotify];
[myNextTask launchTask];
// ...etc
The second pair of setStandard methods cause an exception: "Exception
raised during posting of notification. Ignored. exception: ***
-[NSConcreteFileHandle fileDescriptor]: Bad file descriptor"
Can anyone point out my mistake?
Yes: wanting to reuse a pipe for successive tasks. The sub-task will
close the pipe and then it is gone (that is why the file-descriptor is
"bad").
From the documentation:
"+ (id)pipe
Returns an NSPipe that's been sent autorelease. Because the returned
object will be deallocated at the end of the current loop, retain the
object or, better yet, use the init method if you intend to keep the
object beyond that point."
and
"- (NSFileHandle *)fileHandleForReading
...You don't need to send closeFile to this object or explicitly
release the object after you're finished using it. The descriptor
represented by this object is deleted, and the object itself is
automatically deallocated when the NSPipe is deallocated."
Doesn't this contradict what you wrote about the sub-task closing the
pipe? If so, is there an error in the documentation? I used the init
method expressly so I could hang onto the pipe object, and all the code
below "// task has terminated" is before the NSTask has been released.
So, it seems like both the task and the pipe should still be around...
-Scott
_______________________________________________
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.