Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is %destructor invoked in BISON?

Tags:

c++

bison

When is %destructor invoked in BISON? I have the following bison code:

%union{
    char * sval; 
    Variable * vval; 
} 

%token VARIABLE 
%token Literal 
%type <vval> Expression VARIABLE 
%type <sval> Literal 

%destructor { delete $$; } <vval> 
%destructor { delete $$; } Literal 

where Variable is a class. I thought that after processing a line, all the Variable objects will be freed, but I can see no destructor invoked. And that will lead straightly to memory leak...

Edit: To be clear; I allocate a new Variable object for a new token, and this token is pushed to the BISON stack. I want to delete the Variable when it is popped by bison and discarded from the stack. I thought that %destructor serves that purpose, but I am not sure anymore..

like image 631
Jakub M. Avatar asked Jun 19 '11 08:06

Jakub M.


2 Answers

From the Bison Manual:

Discarded symbols are the following:

  • stacked symbols popped during the first phase of error recovery,
  • incoming terminals during the second phase of error recovery,
  • the current lookahead and the entire stack (except the current right-hand side symbols) when the parser returns immediately, and
  • the start symbol, when the parser succeeds.

So if you don't hit an error, the %destructor will be called on the stack if you return immediately (call YYABORT or YYACCEPT), or it will call it on start symbol if parsing succeeds.

like image 117
John Carter Avatar answered Oct 22 '22 21:10

John Carter


I figured out, that I should free() it after I perform the action, so for example

...
| String CONCAT String { $$ = concat($1,$3); free($1); free($3); }
...

That did the trick for me.

like image 24
Jakub M. Avatar answered Oct 22 '22 23:10

Jakub M.