Search code examples
cvalgrindsocketpair

Should the socketpair ends be closed before application exits?


I have an application and would like to find out if I leak file descriptors, when I use valgrind -track-fds=yes ./app

I get:

 ==18807== FILE DESCRIPTORS: 4 open at exit.
 ==18807== Open AF_UNIX socket 20: <unknown>

Is this considered a file descriptor leak? Should one close socketPair ends when the application terminates or does Linux kernel take care of it?


Solution

  • Normally only an unbounded loss of resources is considered a leak.

    If you open a file in a loop and never close it, you have a leak. Leaks are bad because your program will run out of whatever resource it's leaking.

    If you open a file once, that's not so much a leak, especially if you need it to the very end of the program. It will be closed automatically by the kernel anyway.

    It is a good practice to close even such files, if only just to keep your valgrind log clean.