Date: Tue, 28 Jan 2003 12:27:36 -0600 (CST) From: "Sean C. Farley" <sean-freebsd@farley.org> To: FreeBSD-gnats-submit@FreeBSD.org Subject: bin/47599: Memory leak on stdout Message-ID: <200301281827.h0SIRadF004042@thor.farley.org>
index | next in thread | raw e-mail
>Number: 47599
>Category: bin
>Synopsis: Memory leak on stdout
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: freebsd-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: sw-bug
>Submitter-Id: current-users
>Arrival-Date: Tue Jan 28 10:30:01 PST 2003
>Closed-Date:
>Last-Modified:
>Originator: Sean C. Farley
>Release: FreeBSD 4.7-STABLE i386
>Organization:
>Environment:
System: FreeBSD thor.farley.org 4.7-STABLE FreeBSD 4.7-STABLE #13: Sat Jan 18 08:12:15 CST 2003 root@thor.farley.org:/usr/obj/usr/src/sys/THOR i386
>Description:
When testing an application for memory leaks, I noticed one that
I could not get rid of. I happened to be allocated from
/usr/src/lib/libc/stdio/makebuf.c on line 72, but only when
printf() (or really any function) used stdout. Adding an
fclose(stdout) to the end of my application fixed the memory
leak.
I realize this is not much of a leak, but I thought it would be
nice to have it either documented or fixed before others run
into this confusing leak.
Should standard I/O streams be closed automatically on exit?
>How-To-Repeat:
I used dmalloc (in the ports) for the testing. The stdio source was
recompiled with dmalloc.h to give the line number and file where memory
was allocated. If you recompile with fclose(stdout), the leak will go
away.
test.c:
#include <dmalloc.h>
#include <stdio.h>
int main(int argc,
char **argv)
{
printf("Hello, World\n");
//fclose(stdout);
exit(0);
}
Makefile for libstdio.a using sources from /usr/src/lib/libc/stdio:
CFLAGS=-include /usr/local/include/dmalloc.h -O -pipe -DLIBC_RCS -DSYSLIBC_RCS -I/usr/local/include -I/usr/src/lib/libc_r/../libc/include -DPTHREAD_KERNEL -D_THREAD_SAFE -I/usr/src/lib/libc_r/uthread -I/usr/src/lib/libc_r/../../include -D_LOCK_DEBUG -D_PTHREADS_INVARIANTS -D__DBINTERFACE_PRIVATE -DINET6 -DPOSIX_MISTAKE -I/usr/src/lib/libc_r/../libc/locale -DBROKEN_DES -DYP -DNDEBUG -g
LIB= stdio
SRCS= _flock_stub.c \
asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c \
fgetc.c fgetln.c fgetpos.c fgets.c fileno.c findfp.c flags.c fopen.c \
fprintf.c fpurge.c fputc.c fputs.c fread.c freopen.c fscanf.c \
fseek.c fsetpos.c ftell.c funopen.c fvwrite.c fwalk.c fwrite.c \
getc.c getchar.c gets.c getw.c makebuf.c mktemp.c perror.c \
printf.c putc.c putchar.c puts.c putw.c refill.c remove.c rewind.c \
rget.c scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c sprintf.c \
sscanf.c stdio.c tempnam.c tmpfile.c tmpnam.c ungetc.c vasprintf.c \
vfprintf.c vfscanf.c vprintf.c vscanf.c vsnprintf.c vsprintf.c \
vsscanf.c wbuf.c wsetup.c
.include <bsd.lib.mk>
compile application:
gcc -Wall -O -pipe -I/usr/local/include test.c stdio/libstdio.a -L/usr/local/lib -ldmalloc
configure dmalloc:
`dmalloc -l logfile -i 100 low`
run:
./a.out
output of logfile:
1043778209: 1: Dmalloc version '4.8.2' from 'http://dmalloc.com/'
1043778209: 1: flags = 0x4e40503, logfile 'logfile'
1043778209: 1: interval = 100, addr = 0, seen # = 0
1043778209: 1: starting time = 1043778209
1043778209: 1: free bucket count/bits:
1043778209: 1: basic-block 4096 bytes, alignment 8 bytes, heap grows up
1043778209: 1: heap: 0x804f000 to 0x8052000, size 12288 bytes (3 blocks)
1043778209: 1: heap checked 0
1043778209: 1: alloc calls: malloc 1, calloc 0, realloc 0, free 0
1043778209: 1: alloc calls: recalloc 0, memalign 0, valloc 0
1043778209: 1: total memory allocated: 4096 bytes (1 pnts)
1043778209: 1: max in use at one time: 4096 bytes (1 pnts)
1043778209: 1: max alloced with 1 call: 4096 bytes
1043778209: 1: max alloc rounding loss: 4096 bytes (50%)
1043778209: 1: max memory space wasted: 0 bytes (0%)
1043778209: 1: final user memory space: basic 2, divided 0, 4096 bytes
1043778209: 1: final admin overhead: basic 1, divided 0, 4096 bytes (33%)
1043778209: 1: final external space: 0 bytes (0 blocks)
1043778209: 1: top 10 allocations:
1043778209: 1: total-size count in-use-size count source
1043778209: 1: 4096 1 4096 1 makebuf.c:72
1043778209: 1: 4096 1 4096 1 Total of 1
1043778209: 1: dumping not-freed pointers changed since 0:
1043778209: 1: not freed: '0x8050008|s1' (4096 bytes) from 'makebuf.c:72'
1043778209: 1: total-size count source
1043778209: 1: 4096 1 makebuf.c:72
1043778209: 1: 4096 1 Total of 1
1043778209: 1: known memory: 1 pointer, 4096 bytes
1043778209: 1: ending time = 1043778209, elapsed since start = 0:00:00
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message
help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200301281827.h0SIRadF004042>
