Often it's not possible to free all memory when exiting from an utility forked child process. I want to suppress memory leak detection for these in a library.
I don't want to burden developers using the library with --show-leak-kinds=all
to have to look at leak reports from a child process where nobody can really do anything about them.
I'm aware that --child-silent-after-fork=yes
on the command line can be used to suppress reports for all forked processes, but this might also suppress reports from the main application and requires the users to remember to always specify this option.
A very common case where this is a problem is in code that uses fork/exec when an error path is triggered that needs to terminate the child process without executing the target executable (maybe some setup failed or maybe the executable was not executable after all). Other use cases might include helper processes that are longer lived (like the dmix process from alsa before the days of pulseaudio or similar code).
As this should be developer friendly i'm looking for solutions that can be implemented in a library without the need to manually add parameters to the valgrind invocation. Possible solutions might include using client requests, monitor commands using client requests or clever coding.
Minimal example:
// build with cc example.c -o example
// valgrind --leak-check=full --show-leak-kinds=all ./example
#include <stdlib.h>
#include <unistd.h>
void something_that_forks() {
pid_t pid = fork();
if (pid == 0) {
_exit(0);
}
}
int main(int argc, char* argv[]) {
void *some_allocation = malloc(1000);
something_that_forks();
free(some_allocation);
return 0;
}
I imagine something_that_forks is in some library. I'm looking for a way to modify this so that valgrind does not report leaks in the child process. The library of course does not know anything about the allocations in the main program by other components.