[antlr-interest] C parser - tree generation - memory problem

Heinrich du Toit heinrich at silasvalley.com
Thu Sep 25 03:37:48 PDT 2008


adding the null check in newPooltree seems to stop the segfault.. I 
think. not 100% sure.

Although the thing still nukes my memory almost completely in the process :(




static    pANTLR3_BASE_TREE
newPoolTree        (pANTLR3_ARBORETUM factory)
{
    pANTLR3_COMMON_TREE    tree;

    // See if we need a new tree pool before allocating a new tree
    //
    if    (factory->nextTree >= ANTLR3_FACTORY_POOL_SIZE)
    {
        // We ran out of tokens in the current pool, so we need a new pool
        //
        newPool(factory);
    }

    // Assuming everything went well - we are trying for performance 
here so doing minimal
    // error checking - then we can work out what the pointer is to the 
next commontree.
    //
    tree   = factory->pools[factory->thisPool] + factory->nextTree;
    factory->nextTree++;

    if (!tree){
        return NULL;
    }

    // We have our token pointer now, so we can initialize it to the 
predefined model.
    //
    ANTLR3_MEMMOVE((void *)tree, (const void *)&(factory->unTruc), 
(ANTLR3_UINT32)sizeof(ANTLR3_COMMON_TREE));

    // The super points to the common tree so we must override the one 
used by
    // by the pre-built tree as otherwise we will always poitn to the 
same initial
    // common tree and we might spend 3 hours trying to debug why - this 
would never
    // happen to me of course! :-(
    //



    tree->baseTree.super    = tree;

    // And we are done
    //

    return  &(tree->baseTree);
}



More information about the antlr-interest mailing list