I'm using NSTask to run a background process, monitor its output, and 
ultimately present a message to the user. All has been fine with this 10.2 
through 10.6, as far as I know. Now under 10.7 I saw a case where my code did 
not receive quite all the stdout output from the background process. I saw this 
once; I haven't tried running it a huge number of times, but it is definitely 
not the common case. (And I've never seen it in dev mode running under the 
debugger, only on a machine without dev tools. Also BTW, the output of the bg 
process is plain ASCII, no worries about multi-byte sequences.) Does anybody 
see anything wrong with this code (severely stripped for legibility):

- (void) doSomeTask: (NSTask *) task
{
        NSTask * task = [[NSTask alloc] init];
        [task setLaunchPath: cmdpath];
        [task setArguments: [self getArgs]];
        [task setStandardError: [NSPipe pipe]];
        [task setStandardOutput: [NSPipe pipe]];
        [task launch];

        curScanTask = [task retain];
        
        curStdOutStr = [[NSMutableString alloc] init];
        curStdErrStr = [[NSMutableString alloc] init];
        
        curStdOut = [[task standardOutput] fileHandleForReading];
        curStdErr = [[task standardError] fileHandleForReading];
        
        [[NSNotificationCenter defaultCenter] 
                addObserver: self selector: @selector( processStdOut: )
                name: NSFileHandleReadCompletionNotification
                object: curStdOut];
                
        [[NSNotificationCenter defaultCenter] 
                addObserver: self selector: @selector( processStdErr: )
                name: NSFileHandleReadCompletionNotification
                object: curStdErr];
        
        [curStdOut readInBackgroundAndNotify];
        [curStdErr readInBackgroundAndNotify];
}


- (void) processStdOut: (NSNotification *) ntc
{
        NSFileHandle *f = [ntc object];
        NSData *d = [[ntc userInfo] objectForKey: 
NSFileHandleNotificationDataItem];

        if( [d length] == 0 )
                [self processPipeClose];
        else
        {
                [f readInBackgroundAndNotify];
                [curStdOutStr appendString: 
                        [[[NSString alloc] initWithData: d encoding: 
NSASCIIStringEncoding] autorelease]];
        }
}


- (void) processStdErr: (NSNotification *) ntc
{
        NSFileHandle *f = [ntc object];
        NSData *d = [[ntc userInfo] objectForKey: 
NSFileHandleNotificationDataItem];

        if( [d length] == 0 )
                [self processPipeClose];
        else
        {
                [f readInBackgroundAndNotify];
                [curStdErrStr appendString: 
                        [[[NSString alloc] initWithData: d encoding: 
NSASCIIStringEncoding] autorelease]];
        }
}


- (void) processPipeClose
{
        [curStdOutStr appendString: 
                [[[NSString alloc] initWithData: [curStdOut 
readDataToEndOfFile] encoding: NSASCIIStringEncoding] autorelease]];
                                                        
        [[NSNotificationCenter defaultCenter] 
                removeObserver: self 
                name: NSFileHandleReadCompletionNotification
                object: curStdOut];

        curStdOut = nil;
        
        [curStdErrStr appendString: 
                [[[NSString alloc] initWithData: [curStdErr 
readDataToEndOfFile] encoding: NSASCIIStringEncoding] autorelease]];

        [[NSNotificationCenter defaultCenter] 
                removeObserver: self 
                name: NSFileHandleReadCompletionNotification
                object: curStdErr];

        curStdErr = nil;
}


-- 
Scott Ribe
scott_r...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to