Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Confuse about AddRef / Release.

Confuse about AddRef / Release.

Scheduled Pinned Locked Moved ATL / WTL / STL
questionc++comgraphicsdata-structures
7 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qur
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • Q 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

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      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 to AddRef() 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

      Q N 2 Replies Last reply
      0
      • M Michael Dunn

        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 to AddRef() 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

        Q Offline
        Q Offline
        qur
        wrote on last edited by
        #3

        What is the good way to create the coclass object that can take care of reference count.

        1 Reply Last reply
        0
        • M Michael Dunn

          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 to AddRef() 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

          N Offline
          N Offline
          Nick Parker
          wrote on last edited by
          #4

          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 an AddRef(); 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

          P 1 Reply Last reply
          0
          • N Nick Parker

            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 an AddRef(); 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

            P Offline
            P Offline
            pba_
            wrote on last edited by
            #5

            ATL abstracts IUnknown implementation in three classes : CComObject / CComAggObject and CComPolyObject - CComObject provides the IUnknown implementation for non-aggregated objects.

            Q 1 Reply Last reply
            0
            • P pba_

              ATL abstracts IUnknown implementation in three classes : CComObject / CComAggObject and CComPolyObject - CComObject provides the IUnknown implementation for non-aggregated objects.

              Q Offline
              Q Offline
              qur
              wrote on last edited by
              #6

              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

              P 1 Reply Last reply
              0
              • Q 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

                P Offline
                P Offline
                pba_
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups