[cfe-dev] Ownership of Stmts and Exprs, and destroying them
Sebastian Redl
sebastian.redl at getdesigned.at
Sat Nov 22 15:36:04 PST 2008
Hi,
As I understand the ownership, any given Expr is owned by the Expr it's
an argument for, which is owned by the Stmt it makes up, which is owned
by the function declaration, and so on, up to the translation unit.
Except for some rare cases, like VLA sizes being owned by the
VariableLengthArrayType, and function default argument expressions being
owned by their FunctionDecl.
Types are owned by ASTContext and live until it is destroyed.
Declarations are owned by their DeclStmt or similar.
Expressions that are currently being built - e.g. the argument
parameters of ActOnBinOp - are essentially unowned. As such, if the Sema
function returns early, without building some object to wrap them, they
leak. In addition, they leak when the parser returns early and abandons
the ExprTys it was carrying around. This is seen as acceptable, because
the program will soon end anyway if there was an error.
Still, not everyone seems to think so. Doug goes out of his way to not
leak these objects in the Sema routines, and asked me not to leak them,
either. Chris thought it great that Doug wasn't leaking, too.
To destroy a Stmt or its derived classes, you don't use delete, but the
Destroy member. This may be overridden in subclasses, and the base
version first recursively calls Destroy for every child statement, and
then commits suicide (delete this). Calling delete yourself is *not*
sufficient.
Is this correct? Because I want to clean this up. I want to make the
Sema routines (and later the parser, too) not leak. So I need to know
exactly how this works.
Sebastian
More information about the cfe-dev
mailing list