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
  1. Home
  2. General Programming
  3. COM
  4. Releasing CComPtr

Releasing CComPtr

Scheduled Pinned Locked Moved COM
questioncomperformancehelpannouncement
3 Posts 2 Posters 0 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.
  • A Offline
    A Offline
    alchong
    wrote on last edited by
    #1

    Well, I have a question about the usuage of CComPtr, but I think I know the answer, but would like to make sure I understand this completely... CComPtr is a smart pointer class for managing COM interface pointers, thus I don't need to worry about calling AddRef for Release methods, since they are automatically called. From what I've read, the Release method will be called when the CComPtr goes out of scope. I'm have the task of maintaining a COM client app (first time with COM) and I was thinking about 'reusing' a particular instance of a CComPtr (no need to declare a new one, since there's one already!) The only thing that had me worried what that by reusing the CComPtr does that screw up the reference counting and create a memory leak... psuedocode below: { CComPtr myIsomething = (some Isomething interface) ...do stuff to the CComPtr... ...lets reuse it... myIsomething = (some other Isomething interface) } Since the myIsomething didn't go out of scope when I set "myIsomething" to another interface, I was worried that it may be an issue. So after a day of digging around this is my conclusion: This is actually OK, since if we look at the definition of this template, the = operator is overloaded as such: T* operator=(T* lp) { return (T*)AtlComPtrAssign((IUnknown**)&p, lp); } When I looked up AtlComPtrAssign, it seems that it will call release on the interface pointer, prior to reassignment to the new interface. If that's true, there shouldn't be any issues with reference counting and I can continue on my merry way?

    L 1 Reply Last reply
    0
    • A alchong

      Well, I have a question about the usuage of CComPtr, but I think I know the answer, but would like to make sure I understand this completely... CComPtr is a smart pointer class for managing COM interface pointers, thus I don't need to worry about calling AddRef for Release methods, since they are automatically called. From what I've read, the Release method will be called when the CComPtr goes out of scope. I'm have the task of maintaining a COM client app (first time with COM) and I was thinking about 'reusing' a particular instance of a CComPtr (no need to declare a new one, since there's one already!) The only thing that had me worried what that by reusing the CComPtr does that screw up the reference counting and create a memory leak... psuedocode below: { CComPtr myIsomething = (some Isomething interface) ...do stuff to the CComPtr... ...lets reuse it... myIsomething = (some other Isomething interface) } Since the myIsomething didn't go out of scope when I set "myIsomething" to another interface, I was worried that it may be an issue. So after a day of digging around this is my conclusion: This is actually OK, since if we look at the definition of this template, the = operator is overloaded as such: T* operator=(T* lp) { return (T*)AtlComPtrAssign((IUnknown**)&p, lp); } When I looked up AtlComPtrAssign, it seems that it will call release on the interface pointer, prior to reassignment to the new interface. If that's true, there shouldn't be any issues with reference counting and I can continue on my merry way?

      L Offline
      L Offline
      Lim Bio Liong
      wrote on last edited by
      #2

      Hello alchong, >>...lets reuse it... >>myIsomething = (some other Isomething interface) Yes, this is OK AFAIK. One reusage problem that I have encountered before is related to QueryInterface(). If you were to do this : ISomething -> QueryInterface(IID_ISomething, (void**)&myIsomething); you will get an assertion failure if myIsomething is already holding onto another interface pointer. You can overcome reusage problems by first assigning myIsomething to NULL as in : myIsomething = NULL; before reusing it. - Bio.

      A 1 Reply Last reply
      0
      • L Lim Bio Liong

        Hello alchong, >>...lets reuse it... >>myIsomething = (some other Isomething interface) Yes, this is OK AFAIK. One reusage problem that I have encountered before is related to QueryInterface(). If you were to do this : ISomething -> QueryInterface(IID_ISomething, (void**)&myIsomething); you will get an assertion failure if myIsomething is already holding onto another interface pointer. You can overcome reusage problems by first assigning myIsomething to NULL as in : myIsomething = NULL; before reusing it. - Bio.

        A Offline
        A Offline
        alchong
        wrote on last edited by
        #3

        I see, by assigning it to NULL, that forces the release on the currently held interface, correct? Thanks for the extra tip!

        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