Why is this NSTask terminationStatus returning a nil value?
I know the task returns a value to standard error, which I can see by entering "echo $?" in the terminal after running the task manually.
Now this code:
[aTask launch];
[aTask waitUntilExit];
int status = [aTask terminationStatus];
looks ok, but gdb says status is a locatio开发者_高级运维n at 0x0 and cannot be accessed. Does anybody know of any bugs in the NSTask object in Xcode? Am I doing something wrong?
Thanks for your responses.
Actually, the task is executing just fine and exiting with an indication of success by returning a status of 0. When you echo the result in the terminal, is it 0?
From a comment on your question:
this is from my gdb, after the lines of code above were executed: (gdb) po status Cannot access memory at address 0x0 – demonslayer319
An int
is not an object; thus, po status
won't work. po
works by sending -description
to the object, but it does so in a slightly different fashion than just a plain old objc_msgSend()
call. Thus, when gdb tries to treat the value 0 as an object's address, gdb detects that it can't possibly be valid because 0
as an address can't be dereferenced.
Try p status
instead.
(And, yes, it could be possible that the task is nil -- that you didn't correctly create the task in the first place -- and, thus, nil-eats-message causes status to be 0)
Are you sure the task you're creating is actually created? In addition to diciu's response, if aTask is nil, -[NSTask terminationStatus]
will return zero. This is because messages to nil who return objects return nil, and those who return primitives return 0, 0.0f, NO, etc.
Usually you'd create an NSTask instance with +[NSTask launchedTaskWithLaunchPath:arguments:]
and a failure to create here is normally due to an improperly-specified path or nil arguments, which generates an exception. Since you didn't mention any exception and didn't post the code you used to create the task, it's hard to say whether this is the problem.
精彩评论