Confuse about AddRef / Release.
-
Hi I am new in atl com and little confuse about the reference management. Here is the code: typedef stack <CComObject <CLayout> *> LAYOUTSTACK; typedef vector <CComObject <CLayout> *> LAYOUTVECTOR; CComObject * oLayout; LAYOUTSTACK sLayout; sLayout.push (m_opLayout); //where m_opLayout is private memeber oLayout = sLayout.top(); oLayout->AddRef(); //question: this statement is required or not and why? m_vLayoutList.push_back(oLayout); //private member of type LAYOUTVECTOR sLayout.pop(); On general do we need to call AddRef if pointer assignment is done for CComObject <>. Thanks Qur
-
Hi I am new in atl com and little confuse about the reference management. Here is the code: typedef stack <CComObject <CLayout> *> LAYOUTSTACK; typedef vector <CComObject <CLayout> *> LAYOUTVECTOR; CComObject * oLayout; LAYOUTSTACK sLayout; sLayout.push (m_opLayout); //where m_opLayout is private memeber oLayout = sLayout.top(); oLayout->AddRef(); //question: this statement is required or not and why? m_vLayoutList.push_back(oLayout); //private member of type LAYOUTVECTOR sLayout.pop(); On general do we need to call AddRef if pointer assignment is done for CComObject <>. Thanks Qur
qur wrote: do we need to call AddRef if pointer assignment is done for CComObject.
CComObject
is not a smart pointer class, so any time you create a new reference to the underlying object, you need toAddRef()
it. --Mike-- If it doesn't move and it should: WD-40. If it moves and it shouldn't: duct tape. 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm -
qur wrote: do we need to call AddRef if pointer assignment is done for CComObject.
CComObject
is not a smart pointer class, so any time you create a new reference to the underlying object, you need toAddRef()
it. --Mike-- If it doesn't move and it should: WD-40. If it moves and it shouldn't: duct tape. 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm -
qur wrote: do we need to call AddRef if pointer assignment is done for CComObject.
CComObject
is not a smart pointer class, so any time you create a new reference to the underlying object, you need toAddRef()
it. --Mike-- If it doesn't move and it should: WD-40. If it moves and it shouldn't: duct tape. 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_HelmMichael Dunn wrote: CComObject is not a smart pointer class, so any time you create a new reference to the underlying object, you need to AddRef() it. Ok, not know hardly anything about COM I have a question off this answer. I assume that
CComObject
is an already wrapped class/object? Otherwise wouldn't you call anAddRef();
function inside your constructor to increment the reference counting? I suppose a more generalized answer to my question would be for me to read more about COM. In due time I suppose. :)
Nick Parker
You see the Standards change. - Fellow co-worker
-
Michael Dunn wrote: CComObject is not a smart pointer class, so any time you create a new reference to the underlying object, you need to AddRef() it. Ok, not know hardly anything about COM I have a question off this answer. I assume that
CComObject
is an already wrapped class/object? Otherwise wouldn't you call anAddRef();
function inside your constructor to increment the reference counting? I suppose a more generalized answer to my question would be for me to read more about COM. In due time I suppose. :)
Nick Parker
You see the Standards change. - Fellow co-worker
-
ATL abstracts IUnknown implementation in three classes : CComObject / CComAggObject and CComPolyObject - CComObject provides the IUnknown implementation for non-aggregated objects.
I have to question: 1- If I chose "yes" in aggregation section of ATL Object Wizard then should a create the coclass object using CComAggObject or CComObject? 2- Do same reference counting rules apply when creating coclass object through CComObject as with interface pointers? Thanx in advance. qur
-
I have to question: 1- If I chose "yes" in aggregation section of ATL Object Wizard then should a create the coclass object using CComAggObject or CComObject? 2- Do same reference counting rules apply when creating coclass object through CComObject as with interface pointers? Thanx in advance. qur
If you choose "yes" for aggregation then your class can run either standalone or aggregated, so you can use both CComObject or CComAggObject ( depending on your needs). And yes , you will have to take care of the object lifetime, addref-ing and releasing properly any object created by CComObject. However, if you just want to create and use an object inside a function you can use CComObjectStack - it doesn't perform reference counting.