help-bison
[Top][All Lists]
Advanced

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

RAII techniques in semantic actions leading to double deletion


From: John Horigan
Subject: RAII techniques in semantic actions leading to double deletion
Date: Wed, 5 Dec 2012 12:43:28 -0800

My C++ grammar file is littered with code like this:

sizetime:
        MODTYPE modification_v2 {
            exp_ptr mod($2);
            driver.lexer->maybeVersion = token::CFDG2;
            if ($1 != ASTmodTerm::size && $1 != ASTmodTerm::time) {
                error(@1, "Syntax error");
            } else {
                static const std::string sizeVar("CF::Size");
                static const std::string timeVar("CF::Time");
                driver.NextParameter($1 == ASTmodTerm::size ? sizeVar
: timeVar, mod, @1, @2);
            }
            $$ = 0;
        }
        ;


exp_ptr is a typedef for std::auto_ptr<expression_t>

The general idea is that the semantic values get automatically deleted
if nothing in the action takes ownership of them. This served me well
until recently, when I had a bug where an action threw an exception.
The std::auto_ptr<> deleted the semantic values and then the parser
caught the exception and tried to delete the semantic values a second
time.

One way to think about this is that it is an error for the action to
delete a semantic value if there is any chance that the action can
exit through an exception (or YYERROR, YYABORT, etc). But I really
prefer to use RAII whenever I can.

Can I solve the problem by changing all of my auto_ptr<> declarations from this:

            exp_ptr mod($2);

to this:

            exp_ptr mod($2); $2 = 0;

to explicitly move ownership from the semantic value stack to my
auto_ptr<>? Would this actually do what I think it does and can I
still use @2 after erasing $2?

-- john



reply via email to

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