I just had one of those spooky moments. You know the ones: you are debugging, and find something totally unexpected…something that shouldn’t even be possible.
What I was doing was running a subprocess from within a Cocoa app I am developing. I was using
NSTask to start a script, and retrieving the output with an
NSPipe. Nothing complicated about that.
Because I am still in the early stages of development, the script I was using was just a stand-in, to make sure everything was working to plan. It simply wrote a property list to standard output, with a few static values in it. I was planning to rewrite this script later such that it invoked the UNIX command
ps, to get information about tasks running on the computer.
To my utter surprise, when I ran the application and examined the script output in the debugger, I saw this:
PID TT STAT TIME COMMAND 711 p1 S+ 0:00.20 -bash
Hmm, that looks nothing like my property list, and, what’s more, it looks awfully like the output format of the
ps command! A quick search of my project revealed no reference whatsoever to
ps. What was going on? Was my Mac psychic? Did it know what I was going to do next?
As with many of these ‘How could it be so?’ debugging moments, the answer turned out to be relatively simple, but it had me spooked for a while. When I initially created the script, I hadinserted a
ps command into it, but had quickly forgotten that I had done this, because I then changed the script to print the property list. The script file resides in a directory that is copied into the Resources folder of the application bundle when the app is built. The problem seems to have been that Xcode did not recognize that the script needed to be recopied into the application when it was modified. A clean build fixed the problem.
The moral of the story: when confronted with something you don’t understand, your first instinct is to ascribe it to some supernatural power, when the more likely explanation is just that Xcode is buggy.