[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Destructor trouble
From: |
Andrew Chanler |
Subject: |
Re: Destructor trouble |
Date: |
Tue, 13 May 2003 03:59:54 -0700 (PDT) |
Howdy,
im new to the help-bison mailing list and now exactly sure how to make
a post but i hope this works!
i thought i would give an example of how i was using the %destructor
feature and a problem i ran into.
heres a few lines of code:
//////////////////////////////////////
%union {
double double_val;
char* string_val;
}
%token <string_val> IDENTIFIER
%type <double_val> exp
%type <string_val> line
%type <string_val> any_identifier
%destructor {if($$){
delete [] $$;
$$=NULL;
}
} IDENTIFIER any_identifier line
%destructor { if((defining_func) &&(yyvaluep->string_val)) {
delete [] yyvaluep->string_val;
yyvaluep->string_val = NULL;
}
} exp
////////////////////////////////
now in my rules, every time i allocated memory for strings i used:
$$ = new char[strlen + 1];
when i set the flag defining_func to true, i always access exp as a
string_val and allocate memory to it in the same way.
as you can see, my destructor rule for 'exp' is kind of odd. above it,
i declared exp as double_val type and that prevented me for accessing
exp as string_val in the destructor. i thought that i should have been
able to write my destructor rule for exp like this:
%destructor { if((defining_func) && ($<string_val>$) {
delete [] $<string_val>$;
$<string_val>$ = NULL;
}
} exp
but bison did not like $<string_val>$ in the destructor code, so i went
and loaded my output file(the .tab.c file) and found the line of code
that was being used in the other destructor.
and it was
yyvaluep->string_val
to access the string value.
so i put that into my destructor rule for exp and then everything
worked like a charm.
i did this project in microsoft visual c++ and used its debugger to
check for memory leaks.
after i got those destructor rules working, all of my memory leaks were
cleared up.
but i thought it was pretty anouying that i couldn't access
$<string_val>$ in the destructor rule.
i hope this helps. %destructor is pretty simple. and i figured out how
to use it with the bison 1.875 manual that comes with the the download
of the binary.
and im pretty new to all the bison/flex stuff(i have been
studying/using it for half of this semester at college) so i hope i
didn't do this incorrectly.
Andrew Chanler
--- Hans Yperman <address@hidden> wrote:
>
> Hello everyone,
>
> i have some trouble with understanding the %destructor feature: if I
> have
> a rule:
>
> %type<pointertype> a b c d
> %destructor {do_something} pointertype
>
> a: b c d {
> if(trouble) YYERROR
> }
>
> Say b,c and d are pointers that should be freed.
> If trouble happens, should i free b, c and d myself, or are they
> cleaned
> by YYERROR? And is it possible to call the destructor in the bison
> code,
> say with something like:
>
> if(trouble){ YYDESTRUCT(b); ...; YYERROR;}
>
> Also, if i write yyerror() so that it longjump()s out of the parser,
> how
> can i clean up the stack? This is an issue, as in my program,
> yyerror()
> is called by some other modules that also should clean up the stack
> (e.g. error in the input stream or the lexer)
>
> Another issue: I run both bison 1.35 (from Debian) and bison 1.875
> (GNU). The last one is so immensely slow compared by the first one,
> spending almost 30 seconds running m4. Does someone know why?
>
> Oh, and one more thing. The bison html documentation on the gnu site
> is
> not up-to-date. I can't find anything about %destructor in it. If
> there
> are syntax errors in my example, i'm sorry (But you get what i mean
> with it, i guess).
>
> Thanks in advance,
>
> Hans.
>
__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com