[Cflowd] Fix for CflowdConfig::Clear() and CflowdCollectorMap::Clear() [was: Fix for memory leak on each config re-read.]

From: Nicholas L. Nigay (nnigay@cboss.ru)
Date: Thu Mar 21 2002 - 06:46:00 PST


There are a few amount of memory new()-ed in LoadConfigFile().
But not all of them are freed.
It causes memory leak each time config is re-readed.

Correct versions of CflowdConfig::Clear() and
CflowdCollectorMap::Clear() follows.

in CflowdConfig.cc.in:
------------------------
void CflowdConfig::Clear()
{
  if (this->_ciscoMap.size() > 0) {
   this->_ciscoMap.Clear();
  }

  if (this->_portList.size() > 0)
     this->_portList.erase(this->_portList.begin(),this->_portList.end());

  if (this->_collectorMap.size() > 0) {
    this->_collectorMap.Clear();
  }
  return;
}

in CflowdCollectorMap.cc
previous version of CflowdCollectorMap::Clear() core dumps whenever
(*(*collmIter).second).IpAddress() is called for CflowdCollector that
were recently delete()-d.
( thanks to "Shvedko Pavel" <shved@mtu.ru>):
----------------------------
#include <string>
#include <vector>

#include "CflowdCollectorMap.hh"

//-------------------------------------------------------------------------
// void CflowdCollectorMap::Clear()
//.........................................................................
//
//-------------------------------------------------------------------------
void CflowdCollectorMap::Clear()
{
   CflowdCollectorMap::iterator collmIter;
   vector<CflowdCollectorMap::iterator> deleteIterVect;

   for (collmIter = (*this).begin(); collmIter != (*this).end();
        collmIter++) {
     if ((*collmIter).second != (CflowdCollector *)NULL) {
       if ((*collmIter).first == (*(*collmIter).second).IpAddress()) {
         deleteIterVect.push_back(collmIter);
       }
     }
   }

   vector<CflowdCollectorMap::iterator>::iterator deleteIter;

   for (deleteIter = deleteIterVect.begin();
        deleteIter != deleteIterVect.end();
        deleteIter++) {
     delete((*(*deleteIter)).second);
     (*(*deleteIter)).second = (CflowdCollector *)NULL;
   }

   (*this).erase((*this).begin(),(*this).end());
   return;
}

PS: Pavel, please confirm. 8-)

-- 
Good luck!
Nicholas L. Nigay


This archive was generated by hypermail 2.1.4 : Mon Mar 25 2002 - 11:16:01 PST