[AGENT++] agent++: Memory leak in MigGroup

dominik.vogt at external.thalesgroup.com dominik.vogt at external.thalesgroup.com
Wed Aug 11 15:04:01 CEST 2010


I've been struggling with a memory leak in the MibGroup class for
several days but can not find a way to fix it without crashing the
application.  The code triggering the leak is in system_group.cpp
(and it is possible that there are similar leaks in other files
using MibGroup).  The sysGroup class is derived from MibGroup and
created like this:

  sysGroup::sysGroup(const char* descr, const Oidx& o, const int services): 
    MibGroup(oidSysGroup, "systemGroup") 
  {
	add(new sysDescr(descr));
	add(new sysObjectID(o));
	add(new sysUpTime());
	...
  }

So the created memory is passed into MibGroup::add() and the
constructor then forgets the pointer to the new memory block.  Now
there are two problems:

1. If adding the item to tle list fails, MibGroup::add returns a
NULL pointer, leaking the memory by losing the last reference to
the memory:

  MibEntryPtr MibGroup::add(MibEntryPtr item)
  { 
	if ((item->type() == AGENTPP_LEAF) &&
	    (item->get_access() == NOACCESS)) {
		...
		return 0;
	}
	...
  }

2. The second problem is much worse.  The items are stored in a
List<MibEntry>.  Although they can me removed manually with
MibGroup::remove, if the MibGroup object is deleted, all items
that are still in the list are leaked.  That is because the
destructor just calls List::clear():

  MibGroup::~MibGroup() 
  {
	...
	content.clear(); 
  }

Clear() odes not free the items.  However, if I change that call
to clearAll() (which is supposed to clear the list and delete the
items), I get a crash at the end of the program when destroying
the Mib object in totally unrelated code.  I suppose this is
because something in the list gets deleted twice.  As an
alternative, I tried to call clearAll() in the destructor of the
sysGroup class, but the program crashes with the same symptoms.

Summary: There is a memory leak (also reported by valgrind), bui I
have no idea how to clean it up.

Ciao

Dominik ^_^  ^_^



More information about the AGENTPP mailing list