Skip navigation.
 
mlRe: running out of NSPipes
FROM : Bob Smith
DATE : Sat Apr 19 09:45:06 2008

On Apr 18, 2008, at 9:58 PM, justin webster wrote:
> I'm pretty sure I've got all objects alloc'd and released correctly.
> this example is the broken one:
>
> - (NSString *)myShellFunction:(NSString *)PID
> {
>     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
>
>     NSTask *ps=[[NSTask alloc] init];
>    //NSPipe *pipe=[NSPipe pipe];
>     NSPipe *pipe=[[NSPipe alloc] init];
>    NSFileHandle *handle;
>     NSString *string;
>     
>         [ps setLaunchPath:@"/bin/ps"];
>         [ps setArguments:[NSArray arrayWithObjects:@"-p", PID, @"-o" , 
> @"pcpu", nil]];
>         [ps setStandardOutput:pipe];
>         handle=[pipe fileHandleForReading];
>         [ps launch];
>         string = [[NSString alloc] initWithData:[handle 
> readDataToEndOfFile] encoding:NSASCIIStringEncoding];
>     
>     [ps waitUntilExit];
>     [ps release];
>     [pipe release];
>     [string release];
>     
>     [pool release];
>
>     return string;
> }


I missed the start of this thread so I'm not sure what the original 
trouble was, but this code sure looks like it has a major problem, 
you've deallocated your return value; that final cleanup section 
should probably be:

   [ps release];
   [pipe release];

   [pool release];

   [string autorelease];
   return(string);

>
>
>
> On 19/04/2008, at 2:21 PM, justin webster wrote:
>

>> I guess it may be a bug.
>> I achieved more or less the same thing bypassing NSTask and NSPipe 
>> and now have no issues with resource management.
>> the trick, I think, was fflush() and pclose(). perhaps NSPipe is 
>> missing some tidy-up code.
>> there probably are more efficient ways of doing this but 
>> efficiency is not really a concern in my case.
>>
>> for the record - here's an example which works:
>>     int f;
>>     for(f=0; f<5000; f++){
>>         fflush(nil);
>>         FILE *rtn = popen([@"ls" cString], [@"r" cString]);
>>         //here we use the output of the pipe
>>         pclose(rtn);
>>     }
>>
>> thanks for the help
>> justin
>>
>> On 19/04/2008, at 12:15 PM, Ken Thomases wrote:
>>

>>> On Apr 18, 2008, at 6:05 PM, justin webster wrote:

>>>> I'm pretty sure I've got everything alloc'ing and releasing in 
>>>> the right way

>>>
>>> I agree.  It all looks correct.  It may very well be a bug in the 
>>> framework.
>>>
>>> One last thing to check: does some part of your code register for 
>>> the NSTaskDidTerminateNotification notification, presumably 
>>> without specifying a task object, but listening for all of them? 
>>> If so, might it be retaining the notification object which is the 
>>> task object?
>>>
>>> If there's no other explanation, I recommend that you file a bug 
>>> at bugreport.apple.com.
>>>
>>>
>>> That said, to accomplish what you need I recommend that you use 
>>> sysctl(3) to get the information from the system directly without 
>>> launching the ps process.  Here's some code from Apple that you 
>>> can adapt: http://developer.apple.com/qa/qa2001/qa1123.html
>>>
>>> Good luck,
>>> Ken
>>>

>>

>
> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> 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/<email_removed>
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlrunning out of NSPipes justin webster Apr 18, 11:23
mlRe: running out of NSPipes Scott Ribe Apr 18, 14:21
mlRe: running out of NSPipes Ken Thomases Apr 18, 21:15
mlRe: running out of NSPipes justin webster Apr 19, 04:21
mlRe: running out of NSPipes Jens Alfke Apr 19, 06:39
mlRe: running out of NSPipes justin webster Apr 19, 06:58
mlRe: running out of NSPipes Bob Smith Apr 19, 09:45
mlRe: running out of NSPipes Jens Alfke Apr 19, 17:20