什么时候在BISON中调用%destructor? [英] When is %destructor invoked in BISON?

查看:607
本文介绍了什么时候在BISON中调用%destructor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时在 BISON 中调用%destructor?我有以下的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...

编辑:要清楚;我为一个新的令牌分配一个新的变量对象,这个令牌被推送到BISON栈。我想要删除变量当它被bison弹出并从堆栈中丢弃。我认为%destructor服务于这个目的,但我不再确定了。

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..

推荐答案

我想出, ()它执行操作后,所以例如

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); }
...

这对我来说很麻烦。

这篇关于什么时候在BISON中调用%destructor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆