[antlr-interest] TokenStreamRewriteEngine and C++ and RefCounters

Andrey R. Urazov a_urazov at mail.ru
Tue Apr 27 12:43:09 PDT 2004


Hi,

On Tue, Apr 27, 2004 at 05:52:22PM +0200, Ric Klaren wrote:
> I got the TokenStreamRewriter going mostly it seems (compared to the
> java version I now only see a small difference in whitespace handling
> probably something small somewhere).
So nice! Soon I will be able to apply and test it :)

> It looks like the hunch was good. I reimplemented RefCount into
> a TokenRefCount that does the right thing and a bit more.

Now that I have been using the ANTLR smart pointers for some time,
I have to admit that ASTRefCount has to be rewritten as well. The
current implementation turned out to be a bit buggy.

As I've found out, `operator=(const T*)' and `operator T*()' are unsafe
(in Boost, for example, these aren't defined at all). When implemented
together, not only they are unsafe, they are rather dangerous since they
can lead to nonshared ownership of a single object and therefore to its
premature deletion.

Consider the code from BaseAST.cpp (the problems I experience now are
caused by the implicit misuse of smart pointers in this function):

void BaseAST::addChild( RefAST c )
{
    if( !c )
        return;

    RefBaseAST tmp = down;

    if (tmp)
    {
        while (tmp->right)
            tmp = tmp->right;
        tmp->right = c;
    }
    else
        down = c;
}

Here when `c' is assigned to `right' or `down', assignment of
`ASTRefCount<AST>' to `ASTRefCount<BaseAST>' is performed and at this
point `c' is implicitly converted to `AST*' and then the `operator=(T*)'
is engaged, thus separating the ownership.

Now I have something like the following in the grammar:

rule
{
    RefAST ast = #[TOKEN];
}
:
    (
        index:expression
        {
            ast->addChild(#index);
        }
    )+
;

And as the new `expression' is assigned to `index', the reference count
for the old falls to zero and the object is deleted instead of staying
in memory until the reference in the tree is released as well. When I
then call getNumberOfChildren() the program hangs :(


Best regards,
  Andrey Urazov



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list