views:

66

answers:

1

Hey guys,

I need a bit of help with NSTask. Also, I am new to Cocoa / Obj-C programming, so please bear with me. I am trying to make a directory. Then, remove it. So here is what I have so far:

NSLog (@"START");

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/mkdir"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"/tmp/TEMP", nil];
[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task setStandardError: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

NSLog (@"MKDIR");

[task launch];
[task waitUntilExit];

NSData *data;
data = [file readDataToEndOfFile];

string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

NSLog (@"OUTPUT:\n%@", string);

[task release];
//EDIT: The following lines should be removed and [string release]; should be added.
[arguments release];
[pipe release];
[file release];
[data release];

My question is if the part towards the end about "release"-ing is correct? If not, can someone help me correct it? Also, if I wanted to do another NSTask of "rmdir", would I just do "task = [[NSTask alloc] init];" and so on for each variable I used or will I need to make new variables? THANKS A LOT!

+1  A: 

First, no, you aren't managing memory correctly (hint: only task is correctly handled above). Read this as it explains all.

Secondly, there is no need to use an NSTask instance to make/delete a directory. You should use NSFileManager instead; again -- the documentation explains all.

bbum
Actually, I was just using mkdir/rmdir as an example. I will be doing this for other commands, such as dd, dns-sd, asr, etc.
hassaanm