help-bison
[Top][All Lists]
Advanced

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

Cleaning up memory in bison ?


From: Arno Wilhelm
Subject: Cleaning up memory in bison ?
Date: Wed, 19 Apr 2006 10:31:58 +0200
User-agent: Mozilla Thunderbird 1.0.7-1.1.fc3 (X11/20050929)

Hello,


I am very new to bison and flex and wonder how to clean up pointers/memory in 
bison properly.

Whenever I copied a string to the variable yylval.str in the flex file like 
this:
{WORD}         { yylval.str = strdup( yytext ); return WORD; }
I got a memory leak cause I did not delete the pointer copied by the strdup() 
function.
Here is the output of valgrind:

> valgrind --leak-check=full rules
...
...
==935== 3 bytes in 1 blocks are definitely lost in loss record 2 of 3
==935==    at 0x401B46D: malloc (vg_replace_malloc.c:149)
==935==    by 0x40D1B9F: strdup (in /lib/tls/libc-2.3.6.so)
==935==    by 0x8048976: yylex (in /RulezParser/flex_bison_test/rules)
==935==    by 0x804990E: yyparse (in /RulezParser/flex_bison_test/rules)
==935==    by 0x804A498: main (in /RulezParser/flex_bison_test/rules)
...


First I tried to solve this problem with the %destructor directive in bison, 
which apparently did not work.
Then I solved the problem in the %action section in bison like this:

WORD EQUAL WORD { $$ = !strcmp( $1, $3 ); free($1); free($3); }

This seems to work cause valgrind does not show any memory leak anymore.
But will this work in case of an syntax error ?
And another question arises: What does the %destructor do and how does it work ?


Can anyone with a bit of knowledge/experience shed some ligth on this issue ?



thanks,
arno





reply via email to

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