Re: Bison, memory leaking (Sam Ravnborg)
4 Jun 1997 22:50:19 -0400

          From comp.compilers

Related articles
Bison, memory leaking (Dennis Bj|rklund) (1997-05-25)
Re: Bison, memory leaking (Charles Fiterman) (1997-05-27)
Re: Bison, memory leaking (Paul David Fox) (1997-05-27)
Re: Bison, memory leaking (1997-06-02)
Re: Bison, memory leaking (1997-06-04)
| List of all articles for this month |

From: (Sam Ravnborg)
Newsgroups: comp.compilers
Date: 4 Jun 1997 22:50:19 -0400
Organization: Image Scandinavia
References: 97-05-274 97-05-306 97-06-009
Keywords: yacc, storage

On 2 Jun 1997 10:29:37 -0400, (Dennis
Bjorklund) wrote:
[snip - memory leak's when bison is in error-recovery mode]

>Before I changed bison I had a linked list with all things that were
>created and together with each object I had a pointer to the right
>destructor to call. Then we get lots of overhead that we don't need: a
>list of objects, problem with destructors, extra overhead when freeing
>memory (going through the linked list).
I have done it in this way too. What I did was to let all the
different class'es inherit from the same base class. The constructor
of the base class added the object to a list of allocated objects.
The destructor was made virtual and therefore the parent destructor
was called correct.

This solution have of course the drawbacks related to speed and size.
Furthermore I did not allow any object to be 'deleted' during the

Here's the code if any is interested:

// Linked list of all AST related classes. Used to free up space.
CAstBase *pAstBaseTop = NULL;

// DeleteAst is used to free up all memory used by the Ast,
// including any
// memory used for CType instances.
// The destructors in the various classes do the actual work.

void DeleteAst()
  Summary: Free all memory used for the AST.

    CAstBase *pAst = pAstBaseTop;
    while ((pAst = pAstBaseTop) != NULL)
        pAstBaseTop = pAst->pNext;
        delete pAst;

// All clases that inherit from CAstBase are linked into a linked list
// when allocated. This allow us to free up ALL allocated items.
// Due to the fact that the parser stops as soon as an error is
// discovered
// and the AST may be inconsistent this base class is provided
// for cleanup.
class CAstBase
    CAstBase *pNext; // Next in list.
    // Constructor link in new items in the front.
        extern CAstBase *pAstBaseTop;
        pNext = pAstBaseTop; pAstBaseTop = this;
    virtual ~CAstBase() {} // Virtual to allow calls to derived class

And someone who used it:

// The upper part of the AST.
class CAst : CAstBase
    /* Default constructor private to avoid illegal use */

    /* Type of CAst. */
    NonTerminalList Nt;

Well, it worked for me. If I am going to improve it, I think there is
an area here to save time and space; one 'visible' pointer, and one
'invisible' due to the virtual function, and thats for each tiny

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.