help-bison
[Top][All Lists]
Advanced

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

Re: Memory leak with reentrant parser


From: Martin Alexander Neumann
Subject: Re: Memory leak with reentrant parser
Date: Fri, 19 Jun 2015 15:28:48 +0200
User-agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.7.0

Hi John,

I guess your "strdup" eats the memory. You need to manage this dynamic
memory somewhere in the parser/your program.

Maybe interesting for you: in case of automated parser recovery, bison
offers %destructor[1] to handle memory of automatically discarded symbols.

[1] http://www.gnu.org/software/bison/manual/html_node/Destructor-Decl.html

Yours
Alex
On 06/19/2015 12:31 AM, John W wrote:
> I have a multithreaded application and was testing my mock code for
> memory leaks.  Running the code below results in rapid memory growth of
> the application.
> 
> It was my understanding that calling yy_delete_buffer(buf, scanner) and
> subsequently yylex_destroy(scanner) would be all I need to do to free up
> the memory?
> 
> Any help would be greatly appreciated.  Thanks!
> 
> 
> 
> flex.l:
> 
> %{
> #include <stdlib.h>
> #include <string.h>
> 
> #include "yacc.tab.h"
> %}
> 
> %option reentrant
> %option bison-bridge
> 
> %%
> 
> [\\&]               return AMP;
> [[:alnum:]]+        yylval->strval=strdup(yytext); return TOK_WORD;
> [=]                 return EQ;
> ^[\\?]              return QM;
> 
> 
> 
> 
> yacc.y:
> 
> %{
> typedef struct yy_buffer_state *YY_BUFFER_STATE;
> 
> extern YY_BUFFER_STATE yy_scan_string(const char *, void *);
> extern void yy_delete_buffer(YY_BUFFER_STATE, void *);
> void  yyerror(void *, char *);
> 
> %}
> 
> %union  {
>             u_node *node;
>             char *strval;
>         };
> 
> %pure-parser
> %lex-param {void *scanner}
> %parse-param {void *scanner}
> 
> %token <strval> TOK_WORD AMP EQ QM
> 
> %type <node> pair
> 
> %%
> 
> grammar     :   QM pair;
> 
> pair        :   TOK_WORD EQ TOK_WORD
>             |   TOK_WORD EQ TOK_WORD AMP pair
>             ;
> 
> %%
> 
> int
> main(void)
> {
>     char string[50] = "?key=value&key2=value2";
>     void *scanner;
> 
>     while(1) {
>         yylex_init(&scanner);
>         YY_BUFFER_STATE buffer = yy_scan_string(string, scanner);
>         yyparse(scanner);
>         yy_delete_buffer(buffer, scanner);
>         yylex_destroy(scanner);
>     }
> 
>     return 0;
> }
> 
> int
> yywrap(void) {
>     return 1;
> }
> 
> void
> yyerror(void *scanner, char *s)
> {
>     fprintf(stderr, "Error: %s\n", s);
> }
> 
> 
> _______________________________________________
> address@hidden https://lists.gnu.org/mailman/listinfo/help-bison
> 



reply via email to

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