views:

132

answers:

2

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 location 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.

+1  A: 

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.

Joshua Nozzi
Yes, I am sure the task is being created properly, because it generates XML files correctly, into the directory it's supposed to, every time I run the code. There were no exceptions thrown, until I tried to use *status*.
demonslayer319
Don't want to delete my answer because it's good to know, but in your situation (the non-nil task and use of "po"), bbum's answer is the correct one.
Joshua Nozzi
+5  A: 

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)

bbum
Saw his comment just now myself - given his attempt to "print object" on an int, yours is definitely the right answer. Wish SO had the ability to "retract" without deleting to preserve "guessing history" but demote an answer in a karma-friendly way (ie, no down-voting).
Joshua Nozzi
Yes, this seems to be my problem. I feel rather silly now. Thanks for the help.
demonslayer319