help-flex
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bug? Flex lexer fails memcheck


From: Josh
Subject: Bug? Flex lexer fails memcheck
Date: Mon, 20 Feb 2006 13:47:22 -0800

I've run into a problem debugging a flex (and bison) compiler: the
compiler always fails Valgrind's Memcheck test because the
yy_buffer_state object allocated in yy_create_buffer is never freed. 
I'm guessing there's only one yy_buffer_state object per invokation of
the program, so maybe this isn't actually a memory leak, but it would
be nice if it could be freed before exit so the generated program
would pass Memcheck.  A reproduction of the error (with a minimal
scanner, in Linux) follows:

memcheck-test.l ------------------------

%{
    int num_lines = 0, num_chars = 0;
%}

%%

\n      ++num_lines; ++num_chars;
.       ++num_chars;

%%

int yywrap()
{
    return 1;
}

main()
{
    yylex();
    printf( "# of lines = %d, # of chars = %d\n",
    num_lines, num_chars );
}

Makefile -------------------------------

.PHONY: test
test :  memcheck-test
        valgrind -v --leak-check=full --show-reachable=yes \
        memcheck-test < memcheck-test.l

memcheck-test : lex.yy.c
        gcc -o memcheck-test -g lex.yy.c

lex.yy.c :      memcheck-test.l
        flex memcheck-test.l

----------------------------------------

$ make
flex memcheck-test.l
gcc -o memcheck-test -g lex.yy.c
valgrind -v --leak-check=full --show-reachable=yes \
memcheck-test < memcheck-test.l
==5887== Memcheck, a memory error detector for x86-linux.
==5887== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==5887== Using valgrind-2.4.0, a program supervision framework for x86-linux.
==5887== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==5887== Valgrind library directory: /usr/lib/valgrind
==5887== Command line
==5887==    memcheck-test
==5887== Startup, with flags:
==5887==    -v
==5887==    --leak-check=full
==5887==    --show-reachable=yes
==5887== Contents of /proc/version:
==5887==   Linux version 2.6.14-1.1656_FC4
(address@hidden) (gcc version 4.0.2 20051125 (Red
Hat 4.0.2-8)) #1 Thu Jan 5 22:13:22 EST 2006
==5887== Reading syms from
/home/joshs/mystuff/troika/phase2/trunk/src/api/parser/test/memcheck-test
(0x8048000)
==5887== Reading syms from /lib/ld-2.3.5.so (0x1B8E4000)
==5887==    object doesn't have any debug info
==5887== Reading syms from /usr/lib/valgrind/stage2 (0xB0000000)
==5887== Reading syms from /lib/ld-2.3.5.so (0xB1000000)
==5887==    object doesn't have any debug info
==5887== Reading syms from /usr/lib/valgrind/vgskin_memcheck.so (0xB7C06000)
==5887== Reading syms from /lib/libc-2.3.5.so (0xB7E66000)
==5887==    object doesn't have any debug info
==5887== Reading syms from /lib/libdl-2.3.5.so (0xB7F8F000)
==5887==    object doesn't have any debug info
==5887== Reading suppressions file: /usr/lib/valgrind/default.supp
==5887==
==5887== Reading syms from /usr/lib/valgrind/vg_inject.so (0x1B903000)
==5887== Reading syms from /usr/lib/valgrind/vgpreload_memcheck.so
(0x1B906000)==5887== Reading syms from /lib/libc-2.3.5.so (0x6B9000)
==5887==    object doesn't have any debug info
==5887== Reading syms from /lib/libdl-2.3.5.so (0x80B000)
==5887==    object doesn't have any debug info
==5887== TRANSLATE: 0x71E41E redirected to 0x1B909198
==5887== TRANSLATE: 0x1B8E4770 redirected to 0x52BFF020
# of lines = 22, # of chars = 243
==5887== TRANSLATE: 0x71C5E8 redirected to 0x1B9096C6
==5887==
==5887== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1)
--5887--
--5887-- supp:   13 dl_relocate_object
==5887== malloc/free: in use at exit: 16426 bytes in 2 blocks.
==5887== malloc/free: 2 allocs, 0 frees, 16426 bytes allocated.
==5887==
==5887== searching for pointers to 2 not-freed blocks.
==5887== checked 69672 bytes.
==5887==
==5887== 16426 bytes in 2 blocks are still reachable in loss record 1 of 1
==5887==    at 0x1B909222: malloc (vg_replace_malloc.c:130)
==5887==    by 0x804962F: yy_flex_alloc (lex.yy.c:1469)
==5887==    by 0x80492B4: yy_create_buffer (lex.yy.c:1149)
==5887==    by 0x80486D1: yylex (lex.yy.c:547)
==5887==    by 0x804968E: main (memcheck-test.l:19)
==5887==
==5887== LEAK SUMMARY:
==5887==    definitely lost: 0 bytes in 0 blocks.
==5887==      possibly lost: 0 bytes in 0 blocks.
==5887==    still reachable: 16426 bytes in 2 blocks.
==5887==         suppressed: 0 bytes in 0 blocks.
--5887--     TT/TC: 0 tc sectors discarded.
--5887--            2094 tt_fast misses.
--5887-- translate: new     2079 (37594 -> 512331; ratio 136:10)
--5887--            discard 0 (0 -> 0; ratio 0:10).
--5887-- chainings: 1255 chainings, 0 unchainings.
--5887--  dispatch: 34935 jumps (bb entries); of them 5111 (14%) unchained.
--5887--            1/2153 major/minor sched events.
--5887-- reg-alloc: 425 t-req-spill, 93156+2690 orig+spill uis,
--5887--            11026 total-reg-rank
--5887--    sanity: 2 cheap, 1 expensive checks.
--5887--    ccalls: 8490 C calls, 55% saves+restores avoided (27908 bytes)
--5887--            11156 args, avg 0.88 setup instrs each (2546 bytes)
--5887--            0% clear the stack (25359 bytes)
--5887--            3819 retvals, 31% of reg-reg movs avoided (2336 bytes)
make: *** [test] Error 34




reply via email to

[Prev in Thread] Current Thread [Next in Thread]