[antlr-interest] Lots of memory leak with TokenStreamHiddenTokenFilter of C++ Target in Antlr 2.7x?

service service at sharpplus.com
Mon May 7 23:51:52 PDT 2007


recently I wanted to use TokenStreamHiddenTokenFilter it works as expected. But when I use boost to do some unit test, the boost reports lots of memory leaks. I do not know whether it is problem of my code or not . So I use the example preserveWhiteSpace shipped with antlr and do a little console project under vc7.1. the boost test framework report same problem. I do not know why. the following is the test program and the memory leak log.

#include "stdafx.h"

#include <iostream>

#include "antlr/CommonASTWithHiddenTokens.hpp"
#include "antlr/CommonHiddenStreamToken.hpp"
#include "InstrLexer.hpp"
#include "InstrParser.hpp"
#include "InstrTreeWalker.hpp"

#include  <boost/test/included/unit_test_framework.hpp>
using namespace boost::unit_test;

ANTLR_USING_NAMESPACE(std)
ANTLR_USING_NAMESPACE(antlr)




void sql_type_test() {

//int _tmain(int argc, _TCHAR* argv[])
//{
	// make lexer that generates CommonHiddenStreamToken's
	InstrLexer lexer(cin);
	lexer.setTokenObjectFactory(&CommonHiddenStreamToken::factory);

	TokenStreamHiddenTokenFilter filter(lexer);
	filter.hide(InstrParser::WS_);
	filter.hide(InstrParser::SL_COMMENT);

	// make parser with custom ASTFactory generating CommonASTWithHiddenTokens
	InstrParser parser(filter);

	// make factory
	ASTFactory my_factory("CommonASTWithHiddenTokens",
		&CommonASTWithHiddenTokens::factory);

	// let the parser initialize the factory
	parser.initializeASTFactory(my_factory);
	// tell the parser about the factory
	parser.setASTFactory(&my_factory);

	try
	{
		// Parse the input statements
		parser.slist();
	}
	catch(ANTLRException& e)
	{
		cerr << "exception: " << e.getMessage() << endl;
//		return -1;
		return ;
	}

	RefAST t = parser.getAST();

	InstrTreeWalker walker;
	walker.setFilter(filter);
	// This is only needed for walkers that modify the tree.
	// so it's kindoff redundant...
	walker.setASTFactory(&my_factory);
	//walker.initializeASTFactory();

	try
	{
		walker.slist(t);
	}
	catch(ANTLRException& e)
	{
		cerr << "exception: " << e.getMessage() << endl;
		//return -1;
		return ;
	}
	//return 0;
}

void InstrTreeWalker::setFilter(TokenStreamHiddenTokenFilter& filter_)
{
	filter = &filter_;
}

/** walk list of hidden tokens in order, printing them out */
void InstrTreeWalker::dumpHidden(RefToken t)
{
	for ( ; t ; t=filter->getHiddenAfter(t) )
	{
		cout << t->getText();
	}
}

void InstrTreeWalker::pr(RefAST p)
{
	cout << p->getText();
	dumpHidden( (RefCommonASTWithHiddenTokens(p))->getHiddenAfter() );
}


test_suite*
init_unit_test_suite( int argc, char* argv[] )
{
	test_suite* test= BOOST_TEST_SUITE( "sql parser test" );

	test->add( BOOST_TEST_CASE( &sql_type_test) );

	return test;
}

log
D:\antlr\275\projects\TestHidden\Debug>testhidden <test.in
// start comment
// another comment

a = 2;
{dbg.invoke("g", ""); g();}
{
        a=3*4;
}
if 3 then {dbg.invoke("f", "4 + b"); f(4 + b);}
else {
        b =a; // assign
}
// final

D:\antlr\275\projects\TestHidden\Debug>testhidden <test.in
Running 1 test case...
// start comment
// another comment

a = 2;
{dbg.invoke("g", ""); g();}
{
        a=3*4;
}
if 3 then {dbg.invoke("f", "4 + b"); f(4 + b);}
else {
        b =a; // assign
}
// final

*** No errors detected
Detected memory leaks!
Dumping objects ->
{356} normal block at 0x00377708, 8 bytes long.
 Data: < v7     > A0 76 37 00 01 00 00 00
{355} normal block at 0x003776A0, 56 bytes long.
 Data: <  Q  w7         > 18 1F 51 00 08 77 37 00 13 00 00 00 0D 00 00 00
{354} normal block at 0x00377668, 8 bytes long.
 Data: < v7     > 00 76 37 00 01 00 00 00
{353} normal block at 0x00377600, 56 bytes long.
 Data: <  Q hv7         > 18 1F 51 00 68 76 37 00 12 00 00 00 0C 00 00 00
{348} normal block at 0x00375868, 8 bytes long.
 Data: < X7     > 00 58 37 00 01 00 00 00
{347} normal block at 0x00375800, 56 bytes long.
 Data: <  Q hX7         > 18 1F 51 00 68 58 37 00 13 00 00 00 0B 00 00 00
{346} normal block at 0x003757C8, 8 bytes long.
 Data: <`W7     > 60 57 37 00 01 00 00 00
{345} normal block at 0x00375760, 56 bytes long.
 Data: <  Q  W7         > 18 1F 51 00 C8 57 37 00 12 00 00 00 0B 00 00 00
{165} normal block at 0x00373C98, 8 bytes long.
 Data: <8=7     > 38 3D 37 00 01 00 00 00
{164} normal block at 0x00373D38, 56 bytes long.
 Data: <  Q  <7         > 18 1F 51 00 98 3C 37 00 12 00 00 00 03 00 00 00
{163} normal block at 0x00373CE8, 32 bytes long.
 Data: <// another comme> 2F 2F 20 61 6E 6F 74 68 65 72 20 63 6F 6D 6D 65
{161} normal block at 0x00373C60, 8 bytes long.
 Data: < ;7     > F8 3B 37 00 02 00 00 00
{160} normal block at 0x00373BF8, 56 bytes long.
 Data: <  Q `<7         > 18 1F 51 00 60 3C 37 00 13 00 00 00 02 00 00 00
{157} normal block at 0x00373BA8, 32 bytes long.
 Data: <// start comment> 2F 2F 20 73 74 61 72 74 20 63 6F 6D 6D 65 6E 74
{155} normal block at 0x00373868, 8 bytes long.
 Data: <@;7     > 40 3B 37 00 01 00 00 00
{154} normal block at 0x00373B40, 56 bytes long.
 Data: <  Q h87         > 18 1F 51 00 68 38 37 00 13 00 00 00 01 00 00 00
Object dump complete.

by the way , rewriteengine in antlr 2.7x c++ target has similar problem, but looks like not as serious as hidden filter.

best regards
chinsho


More information about the antlr-interest mailing list