4

这是我获取 Finder 进程的 pid 的 C 方法。GetProcessInformation() 导致段错误。为什么?

这是功能:

static OSStatus
GetFinderPID(pid_t *pid)
{
    ProcessSerialNumber psn = {kNoProcess, kNoProcess};
    ProcessInfoRec info;
    OSStatus status = noErr;

    info.processInfoLength = sizeof(ProcessInfoRec);
    info.processName = nil;

    while (!status)
    {
        status = GetNextProcess(&psn);
        if (!status)
        {
            status = GetProcessInformation(&psn, &info);
        }
        if (!status &&
            info.processType == 'FNDR' &&
            info.processSignature == 'MACS')
        {
            return GetProcessPID(&psn, pid);
        }
    }

    return status;
}

这是回溯:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000032aaaba7
0x00007fffffe00623 in __bzero ()
(gdb) bt
#0  0x00007fffffe00623 in __bzero ()
#1  0x00007fff833adaed in CreateFSRef ()
#2  0x00007fff833ab53b in FSPathMakeRefInternal ()
#3  0x00007fff852fc32d in _CFGetFSRefFromURL ()
#4  0x00007fff852fbfe0 in CFURLGetFSRef ()
#5  0x00007fff85dd273f in GetProcessInformation ()
#6  0x0000000100000bef in GetFinderPID [inlined] () at /path/to/main.c:21
4

2 回答 2

5

将 ProcessInfoRect 结构清零是可行的,而不是尝试确定应将哪些单个字段清零。

于 2010-04-07T01:57:31.723 回答
2

如果您不希望它返回,则需要将 processAppSpec 设置为 nil,否则那里的指针无效。

于 2010-04-07T02:01:35.857 回答