[Ns-developers] ns-3.6 Daily Bug Status for Thursday, October 15, 2009
Andrey Mazo
mazo at iitp.ru
Fri Oct 16 08:35:09 PDT 2009
>> It may be not so clear, that creating a callback will increase reference
>> counter on a particular object.
> It depends on how MakeCallback is called. When making method callbacks, you
> can you plain C++ pointers:
>
> m_myCallback = MakeCallback(&MyClass::MyMethod, this)
>
> Or you can make use of smart pointers:
>
> Ptr<SomeClass> obj = ...;
> m_myCallback = MakeCallback(&SomeClass::MyMethod, obj)
>
> In the former case, the callback does not keep the object alive, while in
> the latter case the m_myCallback member keeps a smart pointer to the
> SomeClass instance 'obj', hence 'obj' is kept alive by the callback. In
> this case, clearing the callback in DoDispose () is recommended to break
> potential reference cycles.
Yes, I already understood this while working on bug 711 and examining all Ref()/Unref()'s.:)
This looks pretty clear and straightforward now.
But this may be not obvious for new users, who tries to write a new model, so I think it worth mentioning in tutorial/manual.
>> For example, wifi code completely ignores destroying callbacks.
>> I go for a memory management chapter for the manual containing something
>> like:
>> """
>> 1) Destructors must:
>> 1.a) free allocated memory with delete/free()
>> 1.b) explicitly cancel all events
>>
>> 2) Destructors should not (exception is when a special order of destructors
>> is required):
>> 2.a) explicitly zero all Ptr's ()
>> 2.b) explicitly clear all stl containers
>>
>> 3) DoDispose () must:
>> 3.a) explicitly zero all Ptr's ()
>> 3.b) explicitly clear all stl containers
>> 3.c) explicitly cancel all events
>> 3.d) explicitly set all callbacks to MakeNullCallback<> ()
>> 3.e) free manually allocated memory with delete/free()
>> """
> Agreed except that I don't think destructors should be doing whatever it is
> DoDispose is already doing; that would be redundant...
I agree with you if both of them exist.
But if DoDispose() is missing, destructor must do the work.
--
Andrey Mazo.
More information about the Ns-developers
mailing list