NSPipes and NSTask, and a file descriptor leak
NSPipes and NSTask, and a file descriptor leak
- Subject: NSPipes and NSTask, and a file descriptor leak
- From: Gerben Wierda <email@hidden>
- Date: Wed, 27 Nov 2002 18:02:51 +0100
(Apologies for people who see this twice, I intended this for this list
but I just noticed that I had posted it on another one).
G
I am having a file descriptor leak in my program because NSTask does
not release NSPipe/FileHandle when it is released if it has not been
run.
Here is a minimalistic program showing my question. It can be compiled
with
cc -o MyTest TestTask.m -framework Cocoa
==================== TestTask.m ====================
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <stdio.h>
#include <unistd.h>
int main( int argc, char *argv[])
{
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
NSTask *task = [[NSTask alloc] init];
[task setStandardInput:[NSPipe pipe]];
[task setStandardOutput:[NSPipe pipe]];
[task setStandardError:[NSPipe pipe]];
system( "/usr/sbin/lsof >lsof.before");
#ifdef RUNNING
[task setLaunchPath:@"/bin/ls"];
[task launch];
[task waitUntilExit];
#endif
[task release];
system( "/usr/sbin/lsof >lsof.after");
[pool release];
}
==================== TestTask.m ====================
When I run ./MyTest and the run "opendiff lsof.before lsof.after" I
notice that the pipes created for the NSTask are still there after I
have released the NSTask. If compiled with
cc -o MyTestRunning -DRUNNING TestTask.m -framework Cocoa
The pipes are closed when I run ./MyTestRunning (as can again be seen
from "opendiff lsof.before lsof.after")
Is this maybe a bug in NSTask? If so, what would be a good workaround?
Closing the filehandles from the pipes if the task was never launched,
closes stdout, stderr and stdin of my app it seems from lsof output --
not good. Terminating the task when it is not running raises an
exception. Somehow, I cannot seem to find a way to setup a task with
pipes, *not* run it and release it and not have a file descriptor leak.
But there might easily be something I do not understand of course.
G
(Reason for this construct: I sometimes have to decide at a late stage
not to run a certain set of created interdependent tasks after all, so
I need to be able to create and release them without this kind of leak.
I am too far in my project to consider redesgning it, let alone that it
is unlikely that a redesign would be feasible given the constraints of
connecting one task to the other (for which the pipes must exist in an
early stage). So unless there is something I have missed, I would
really welcome a workaround).
_______________________________________________
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.