convert ChannelCoordinationListener to a reference counted object to fix memory issues
authorTom Henderson <tomh@tomh.org>
Tue, 27 Jan 2015 21:47:30 -0800
changeset 11166 96cef68fae19
parent 11165 d390d8eaafe2
child 11167 89730ec95897
convert ChannelCoordinationListener to a reference counted object to fix memory issues
src/wave/model/channel-coordinator.cc
src/wave/model/channel-coordinator.h
src/wave/model/default-channel-scheduler.cc
src/wave/model/default-channel-scheduler.h
src/wave/test/mac-extension-test-suite.cc
--- a/src/wave/model/channel-coordinator.cc	Tue Jan 27 21:53:05 2015 +0100
+++ b/src/wave/model/channel-coordinator.cc	Tue Jan 27 21:47:30 2015 -0800
@@ -295,7 +295,7 @@
 }
 
 void
-ChannelCoordinator::RegisterListener (ChannelCoordinationListener *listener)
+ChannelCoordinator::RegisterListener (Ptr<ChannelCoordinationListener> listener)
 {
   NS_LOG_FUNCTION (this << listener);
   NS_ASSERT (listener != 0);
@@ -303,7 +303,7 @@
 }
 
 void
-ChannelCoordinator::UnregisterListener (ChannelCoordinationListener *listener)
+ChannelCoordinator::UnregisterListener (Ptr<ChannelCoordinationListener> listener)
 {
   NS_LOG_FUNCTION (this << listener);
   NS_ASSERT (listener != 0);
--- a/src/wave/model/channel-coordinator.h	Tue Jan 27 21:53:05 2015 +0100
+++ b/src/wave/model/channel-coordinator.h	Tue Jan 27 21:47:30 2015 -0800
@@ -26,7 +26,7 @@
 /**
  * \brief receive notifications about channel coordination events.
  */
-class ChannelCoordinationListener
+class ChannelCoordinationListener : public SimpleRefCount<ChannelCoordinationListener>
 {
 public:
   virtual ~ChannelCoordinationListener (void);
@@ -186,13 +186,13 @@
    * Add the input listener to the list of objects to be notified of
    * channel coordination events.
    */
-  void RegisterListener (ChannelCoordinationListener *listener);
+  void RegisterListener (Ptr<ChannelCoordinationListener> listener);
   /**
    * \param listener the current attached listener
    *
    * Remove the specified listener.
    */
-  void UnregisterListener (ChannelCoordinationListener *listener);
+  void UnregisterListener (Ptr<ChannelCoordinationListener> listener);
   /**
    * Remove all listeners.
    */
@@ -227,8 +227,8 @@
   Time m_schi;  // SchInterval
   Time m_gi;    // GuardInterval
 
-  typedef std::vector<ChannelCoordinationListener *> Listeners;
-  typedef std::vector<ChannelCoordinationListener *>::iterator ListenersI;
+  typedef std::vector<Ptr<ChannelCoordinationListener> > Listeners;
+  typedef std::vector<Ptr<ChannelCoordinationListener> >::iterator ListenersI;
   Listeners m_listeners;
 
   uint32_t m_guardCount;
--- a/src/wave/model/default-channel-scheduler.cc	Tue Jan 27 21:53:05 2015 +0100
+++ b/src/wave/model/default-channel-scheduler.cc	Tue Jan 27 21:47:30 2015 -0800
@@ -90,7 +90,6 @@
   m_coordinator = 0;
   if (m_coordinationListener != 0)
     {
-      delete m_coordinationListener;
       m_coordinationListener = 0;
     }
   if (!m_waitEvent.IsExpired ())
@@ -117,7 +116,7 @@
   // since default channel scheduler is in the context of single-PHY, we only use one phy object.
   m_phy = device->GetPhy (0);
   m_coordinator = device->GetChannelCoordinator ();
-  m_coordinationListener = new CoordinationListener (this);
+  m_coordinationListener = Create<CoordinationListener> (this);
   m_coordinator->RegisterListener (m_coordinationListener);
 }
 
--- a/src/wave/model/default-channel-scheduler.h	Tue Jan 27 21:53:05 2015 +0100
+++ b/src/wave/model/default-channel-scheduler.h	Tue Jan 27 21:47:30 2015 -0800
@@ -113,7 +113,7 @@
   uint32_t m_waitChannelNumber;
   uint32_t m_waitExtend;
 
-  ChannelCoordinationListener *m_coordinationListener;
+  Ptr<ChannelCoordinationListener> m_coordinationListener;
 };
 
 }
--- a/src/wave/test/mac-extension-test-suite.cc	Tue Jan 27 21:53:05 2015 +0100
+++ b/src/wave/test/mac-extension-test-suite.cc	Tue Jan 27 21:47:30 2015 -0800
@@ -210,7 +210,8 @@
 
   m_coordinator = CreateObject<ChannelCoordinator> ();
   // third test channel coordination events
-  m_coordinator->RegisterListener (new CoordinationTestListener (this));
+  Ptr<CoordinationTestListener> ptr = Create<CoordinationTestListener> (this);
+  m_coordinator->RegisterListener (ptr);
   Simulator::Stop (Seconds (100.0));
   Simulator::Run ();
   Simulator::Destroy ();