2010-04-07 63 views
4

這裏是我的C方法來獲取Finder進程的pid。 GetProcessInformation()導致段錯誤。爲什麼?OSX:爲什麼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 

回答

5

歸零的ProcessInfoRect結構的工作,而不是試圖確定哪個單個字段應該爲零。

+0

我初始化'procinfo'如下: bzero(&procInfo,sizeof(ProcessInfoRec)); procInfo.processInfoLength = sizeof(ProcessInfoRec); – 2012-02-22 15:31:08

+2

procinfo = {0}; – anthony 2012-02-24 06:14:59

2

如果你不想返回,你需要設置processAppSpec爲nil,否則你有一個無效的指針。

+1

processAppSpec在Snow Leopard上似乎不存在 – anthony 2010-04-07 07:23:43

+0

Snow Leopard中不存在FSSpecs。 – 2010-04-07 17:42:45

相關問題