Valgrind – dynamic code analysis tool – part II – fd leak

Valgrind – File descriptor (fd) leak detection trial

# File descriptor (fd) leak detection
--track-fds=yes
[root@localhost valgrindTest]# cat fdLeakTry.c
/* daveti's stupid code for fd leak */
#include <stdio.h>
int main( int argc, char *argv[])
{
 FILE *fdPtr = fopen("daveti.log", "rb");
 if ( argc != 1)
 {
 printf("FD leaking!n");
 return 1;
 }
 fclose(fdPtr);
 return 0;
}
[root@localhost valgrindTest]# gcc -Wall -g -o fdLeakTry fdLeakTry.c
[root@localhost valgrindTest]# valgrind --track-fds=yes --log-file=./fdLeakTry.log ./fdLeakTry hello
FD leaking!
[root@localhost valgrindTest]# cat fdLeakTry.log
==17725== Memcheck, a memory error detector
==17725== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==17725== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==17725== Command: ./fdLeakTry hello
==17725== Parent PID: 4801
==17725==
==17725==
==17725== FILE DESCRIPTORS: 5 open at exit.
==17725== Open file descriptor 4: daveti.log
==17725== at 0x6C85A3: __open_nocancel (in /lib/libc-2.5.so)
==17725== by 0x669AA4: _IO_file_fopen@@GLIBC_2.1 (in /lib/libc-2.5.so)
==17725== by 0x65E75C: __fopen_internal (in /lib/libc-2.5.so)
==17725== by 0x65E7BB: fopen@@GLIBC_2.1 (in /lib/libc-2.5.so)
==17725== by 0x804843B: main (fdLeakTry.c:5)
==17725==
==17725== Open file descriptor 3: /home/daveti/valgrindTest/fdLeakTry.log
==17725==    <inherited from parent>
==17725==
==17725== Open file descriptor 2: /dev/pts/1
==17725==    <inherited from parent>
==17725==
==17725== Open file descriptor 1: /dev/pts/1
==17725==    <inherited from parent>
==17725==
==17725== Open file descriptor 0: /dev/pts/1
==17725==    <inherited from parent>
==17725==
==17725==
==17725== HEAP SUMMARY:
==17725==     in use at exit: 352 bytes in 1 blocks
==17725==   total heap usage: 1 allocs, 0 frees, 352 bytes allocated
==17725==
==17725== LEAK SUMMARY:
==17725==    definitely lost: 0 bytes in 0 blocks
==17725==    indirectly lost: 0 bytes in 0 blocks
==17725==      possibly lost: 0 bytes in 0 blocks
==17725==    still reachable: 352 bytes in 1 blocks
==17725==         suppressed: 0 bytes in 0 blocks
==17725== Rerun with --leak-check=full to see details of leaked memory
==17725==
==17725== For counts of detected and suppressed errors, rerun with: -v
==17725== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 8)
[root@localhost valgrindTest]#

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Static Code Analysis and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s