Question

Voici ma méthode C pour obtenir le pid du processus du Finder. GetProcessInformation () provoque une erreur de segmentation. Pourquoi?

Voici la fonction:

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;
}

Voici le backtrace:

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
Était-ce utile?

La solution

Mise à zéro le struct ProcessInfoRect a travaillé, plutôt que d'essayer de déterminer quels champs individuels doivent être mis à zéro.

Autres conseils

Vous devez définir processAppSpec à zéro si vous ne voulez retourné, sinon vous avez là un pointeur non valide.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top