• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string


  • Subject: Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
  • From: "Shawn Erickson" <email@hidden>
  • Date: Mon, 27 Aug 2007 13:33:35 -0700

On 8/27/07, Randall Meadows <email@hidden> wrote:
> At 3:43 PM -0400 8/27/07, Gonzalo Castro wrote:
>
> >         [myTask launch];
> >
> >         NSString *fileContents = [NSString stringWithContentsOfFile:
> >path encoding: NSUTF8StringEncoding error: nil];
> >         NSLog(@"    fileContents = \"%@\"", fileContents);
> >
> >         [myTask autorelease];
> >       }
> >
> >If I put a breakpoint at the top and step trough this code I do get
> >the file contents in the NSString. However, if I run without
> >breakpoints I get an empty string yet when I inspect /tmp/myFile.tmp
> >via the Terminal, it has the expected content.
> >
> >Is there some file system flushing I need to do?
>
> My guess is, the task has not finished by the time the next line has
> executed.  By breaking in the debugger, and via Terminal inspection,
> the task has finished and the output available.  You can probably
> test this by putting a sleep() call after launch, but I'm not sure
> the best way to correctly handle the situation.
>
> Actually, yes, I do (after looking up NSTask in the dev docs).  After
> the [myTask launch], put a [myTask waitUntilExit] call; this will
> ensure the task has completed and any output you're expecting will be
> available.

In the code posted use of waitUnitExit may cause a deadlock. If the
task fills the standard output pipe (stdout) the tool will block and
stdout wont get drained because the code above doesn't drain it until
waitUnitExit returns. The result is that both processes just stall
waiting on the other to do something.

You should drain stdout using the asynchronous read methods that
NSFileHandle provides (for example readInBackgroundAndNotify) and when
end of the pipe is reached you know that the task has terminated.

...or he could have the task output to a file, waitUnitExit on that
task, then read in the file generated.

-Shawn
_______________________________________________

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

  • Follow-Ups:
    • Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
      • From: Rosyna <email@hidden>
    • Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
      • From: "Shawn Erickson" <email@hidden>
References: 
 >NSTask setStandardOutput: then stringWithContentsOfFile gives empty string (From: Gonzalo Castro <email@hidden>)

  • Prev by Date: Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
  • Next by Date: Re: CGContextSetStyle console log error?
  • Previous by thread: Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
  • Next by thread: Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
  • Index(es):
    • Date
    • Thread