[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: %destructor
From: |
Bob Rossi |
Subject: |
Re: %destructor |
Date: |
Fri, 26 Sep 2014 15:58:57 -0400 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
On Fri, Sep 26, 2014 at 03:37:18PM +0200, Hans Aberg wrote:
> > On 26 Sep 2014, at 03:31, Bob Rossi <address@hidden> wrote:
>
> > A secondary concern I had with %destructor is how it worked when lists
> > are used in the bison grammar. I'm concerned about a double free. For
> > instance,
> > result_list: {
> > $$ = NULL;
> > };
> >
> > result_list: result_list COMMA result {
> > $$ = append_gdbmi_result ($1, $3);
> > };
> >
> > result: variable {
> > $$ = gdbmi_result_alloc();
> > $$->variable = $1;
> > };
> >
> > variable: STRING_LITERAL {
> > char *text = gdbmi_get_text(yyscanner);
> > $$ = strdup(text);
> > };
> >
> > %destructor { gdbmi_result_free($$); } result_list
> > %destructor { gdbmi_result_free($$); } result
> > %destructor { free($$); } variable
> >
> > The %destructor for result and result_list does not call free, but
> > instead calls gdbmi_result_free. gdbmi_result_free free's the result
> > (including the variable member of the result), and the result's next
> > pointer, and so on.
> >
> > Is it correct for me to call gdbmi_result_free($$) instead of free($$)?
>
> The %destructor is for making C style cleanup during an error recovery: the
> Bison generated parser will then skip forward, bypassing any normal
> deallocations in the actions. So first write the parser in normal C style,
> with deallocators in each instance matching the allocator used in the
> actions. Then add %destructor if needed for the error recovery.
Unfortunately, this confused me more than clarified things.
What are the 'normal deallocations in the actions?' I only ever put
allocations in the actions, never deallocations.
In the second sentence you mentioned I should write deallocators in each
instance matching the allocator used in the actions. THEN add
%destructor if needed for error recovery.
Where do I put deallocators in the bison generator parser, besides
%destructor?
All of this side stepped the original question. Sorry if I was unclear.
The question I was asking is, should I call free() or list_free() in
the %destructor for a list. I ran valgrind with only free() and noticed
that I was leaking memory still. When I changed it to list_free() the
parser no longer leaked memory on error.
It's my suspicion that calling list_free() is correct because the $$
might have been built up as a list, and %destructor may not be called
on each parse rule that helped build the list. I just wanted to get
confirmation on this from the Bison folk themselves.
Can I Bison maintainer help me out?
Thanks,
Bob Rossi
Re: %destructor, Bob Rossi, 2014/09/29