3

I am trying to check memory leaks on a C program containing child processes using the "leaks -atExit -- ./PROGRAM_NAME" command. Note that the program returns normally when executed on its own. The leaks command fails to return when the program contains the waitpid() function. Why?

Below is a minimal code example that generates the behavior. Thanks for your help.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main()
{
    pid_t   pid;

    pid = fork();
    if (pid == -1)
    {
        printf("Fork error\n");
        exit(EXIT_FAILURE);
    }
    if (pid == 0)
    {
        printf("Hello from the child process\n");
        exit(EXIT_SUCCESS);
    }
    printf("Hello from the parent process\n");
    if (waitpid(pid, NULL, 0) == -1)
    {
        printf("Waitpid error\n");
        exit(EXIT_FAILURE);
    }
    printf("Child process finished\n");
    return (0);
}
BlueJohn
  • 31
  • 2
  • 1
    `exit(EXIT_SUCCESS);` from the child process might be causing problems for `leaks` since it will flush all `FILE *` output buffers inherited from the parent process along with calling any exit handlers installed by `leaks`. Replace it with `_exit(EXIT_SUCCESS);` – Andrew Henle Jan 15 '23 at 13:54
  • Thanks for the suggestion. I just tried it but it does not change the issue unfortunately. – BlueJohn Jan 15 '23 at 16:23

0 Answers0