[antlr-interest] How to free ANTLR3_STRING (c runtime)?
Chetty, Jay
jay.chetty at intel.com
Fri May 15 11:22:32 PDT 2009
Jim thanks for the reply.
My understanding was also that string factory will manage the life time of the strings it created.
But ran into out of memory issue on a 32 bit machine, so thought of freeing up some strings that
Were created while forward looking some tokens during error reporting.
As you suggested I will access the token structure directly.
And thanks for info on dot generator , I will give it a try.
Note:
By the way the toStringSS() function doesn't check for the pointer returned by the string factory
(so and seg faults when out of memory).
static pANTLR3_STRING
toStringSS (pANTLR3_TOKEN_STREAM ts, ANTLR3_UINT32 start, ANTLR3_UINT32 stop)
{
...
string = tsource->strFactory->newRaw(tsource->strFactory);
for (i = start; i <= stop; i++)
{
tok = ts->get(ts, i);
if (tok != NULL)
{
string->appendS(string, tok->getText(tok));
}
}
...
}
Thanks
+Jay
-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jim Idle
Sent: Friday, May 15, 2009 10:54 AM
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] How to free ANTLR3_STRING (c runtime)?
Chetty, Jay wrote:
> What's the right way to free the ANTLR3_STRING returned from
> pANTLR3TOKEN_STREAM's toStringSS()
>
>
> I used destroy method of the string factory and it seems that's not the right way
> As I get these lines when my executable exists.
>
> *** glibc detected *** double free or corruption (fasttop): 0x081514b8 ***
> *** glibc detected *** double free or corruption (!prev): 0x081641a8 ***
>
The correct way is to do nothing at all:-). The string factory tracks
the memory and releases it once you close the factory. The factory will
be created by the token or node stream and will be closed when you free
the token or node stream.
Note that the string factory stuff is basically convenience methods for
things like toStringTree(). If you need pure performance, then you
should access the token structures yourself.
You can use valgrind to check that all your memory is being freed correctly.
Also note that while making a string from the AST is a quick way of
looking at your tree, you might find the dot generator and the dot
program much more useful (install graphviz from your distro or
www.graphviz.org).
To use it from C, you just do this:
pANTLR3_STRING dotSpec;
dotSpec = nodes->adaptor->makeDot(nodes->adaptor, psrReturn.tree);
Where nodes is the pANTLR3_COMMON_TREE_NODE_STREAM and psrReturn is the
return type of the rule you invoke on your parser.
You can then fwrite the spec to a text file:
dotSpec = dotSpec->toUTF8(dotSpec); // Only need this if your input
was not 8 bit characters
fwrite((const void *) dotSpec->chars, 1, (size_t) dotSpec->len, dotFILE);
Then turn this in to a neat png graphic like this:
sprintf(command, "dot -Tpng -o%spng %sdot", dotFname, dotFname);
system(command)
Jim
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
More information about the antlr-interest
mailing list