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

Frank Fock fock at agentpp.com
Wed Aug 11 15:27:45 CEST 2010


Hi Dominik,
Only the first issue (not freeing the memory if adding Mib object  
fails) is a real leak and will be fixed in the next release. The other  
one is not a leak, because the Mib class is always responsible for  
deleting MibEntry objects.
Best regards,
Frank


Am 11.08.2010 um 15:04 schrieb <dominik.vogt at external.thalesgroup.com>:

> 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 ^_^  ^_^
> _______________________________________________
> AGENTPP mailing list
> AGENTPP at agentpp.org
> http://lists.agentpp.org/mailman/listinfo/agentpp



More information about the AGENTPP mailing list