Difference between revisions of "HOWTO resolve circular references in ns-3 memory disposal"

From Nsnam
Jump to: navigation, search
(HOWTO resolve smart pointer circular references in ns-3 memory disposal)
(HOWTO resolve smart pointer circular references in ns-3 memory disposal)
Line 5: Line 5:
 
     public:
 
     public:
 
         static TypeId GetTypeId (void);
 
         static TypeId GetTypeId (void);
        void CallbackMethodA();
 
 
         Callback<void> m_callback;
 
         Callback<void> m_callback;
 
     };
 
     };
Line 12: Line 11:
 
     public:
 
     public:
 
         static TypeId GetTypeId (void);
 
         static TypeId GetTypeId (void);
         void CallbackMethodB();
+
         void CallbackMethodB (void);
        Callback<void> m_callback;
+
 
     };
 
     };
    <pre>
 
    int main(int argc, char* argv[])
 
    {
 
        Ptr<A> a = CreateObject<A>();
 
        Ptr<B> b = CreateObject<B>();
 
        a->m_callback = MakeCallback (&B::CallbackMethodB, b);
 
        b->m_callback = MakeCallback (&A::CallbackMethodA, a);
 
    }
 
    </pre>
 
or
 
 
     <pre>
 
     <pre>
 
     int main(int argc, char* argv[])
 
     int main(int argc, char* argv[])
Line 40: Line 28:
 
     ==15749==    still reachable: 10,360 bytes in 5 blocks
 
     ==15749==    still reachable: 10,360 bytes in 5 blocks
 
     ==15749==    suppressed: 0 bytes in 0 blocks
 
     ==15749==    suppressed: 0 bytes in 0 blocks
 +
 +
The preferred way to handle reference cycles like this in ns-3 is to use Object::Dispose().  This method will call the DoDispose method on the object that it is called on as well as all other objects aggregated on to it.

Revision as of 17:43, 30 August 2013

HOWTO resolve smart pointer circular references in ns-3 memory disposal

    class A : public Object
    {
    public:
       static TypeId GetTypeId (void);
       Callback<void> m_callback;
    };
    class B : public Object
    {
    public:
       static TypeId GetTypeId (void);
       void CallbackMethodB (void);
    };
     int main(int argc, char* argv[])
     {
        Ptr<A> a = CreateObject<A>();
        Ptr<B> b = CreateObject<B>();
        a->m_callback = MakeCallback (&B::CallbackMethodB, b);
        b->AggregateObject(a);
     }
     
    ==15749== LEAK SUMMARY:
    ==15749==    definitely lost: 40 bytes in 1 blocks
    ==15749==    indirectly lost: 152 bytes in 5 blocks
    ==15749==    possibly lost: 0 bytes in 0 blocks
    ==15749==    still reachable: 10,360 bytes in 5 blocks
    ==15749==    suppressed: 0 bytes in 0 blocks

The preferred way to handle reference cycles like this in ns-3 is to use Object::Dispose(). This method will call the DoDispose method on the object that it is called on as well as all other objects aggregated on to it.