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. COM Question

COM Question

Scheduled Pinned Locked Moved COM
c++questioncsharpcomannouncement
3 Posts 3 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.
  • N Offline
    N Offline
    Nick Parker
    wrote on last edited by
    #1

    I have been reading Visual C++.NET by George Shepherd and I ran into something that didn't quite jive with me. Look at the following first:

    struct CSpaceship : IMotion. IDisplay
    {
    ULONG m_cRef;
    //...

    HRESULT QueryInterface(REFIID riid,
    void** ppv);
    ULONG AddRef()
    {
    return InterlockedIncrement(&m_cRef);
    }

    ULONG Release()
    {
    ULONG cRef = InterlockedIncrement(&m_cRef);
    if(cRef == 0)
    {
    delete this;
    return 0;
    }
    else
    return m_cRef;
    }

    //...
    };

    Possibly this isn't a complete implementation of Release(); but shouldn't m_cRef be decremented somewhere in Release();? I can't see how the InterlockedIncrement(); could do this because it is being called in AddRef(); as well as in the Release(); method. -Nick Parker

    V A 2 Replies Last reply
    0
    • N Nick Parker

      I have been reading Visual C++.NET by George Shepherd and I ran into something that didn't quite jive with me. Look at the following first:

      struct CSpaceship : IMotion. IDisplay
      {
      ULONG m_cRef;
      //...

      HRESULT QueryInterface(REFIID riid,
      void** ppv);
      ULONG AddRef()
      {
      return InterlockedIncrement(&m_cRef);
      }

      ULONG Release()
      {
      ULONG cRef = InterlockedIncrement(&m_cRef);
      if(cRef == 0)
      {
      delete this;
      return 0;
      }
      else
      return m_cRef;
      }

      //...
      };

      Possibly this isn't a complete implementation of Release(); but shouldn't m_cRef be decremented somewhere in Release();? I can't see how the InterlockedIncrement(); could do this because it is being called in AddRef(); as well as in the Release(); method. -Nick Parker

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      Interesting. Maybe it it is an error. Otherwise, the server is testing to make sure the client has released all pointers and m_cRef is actually at zero. One situation is multithreading. Kuphryn

      1 Reply Last reply
      0
      • N Nick Parker

        I have been reading Visual C++.NET by George Shepherd and I ran into something that didn't quite jive with me. Look at the following first:

        struct CSpaceship : IMotion. IDisplay
        {
        ULONG m_cRef;
        //...

        HRESULT QueryInterface(REFIID riid,
        void** ppv);
        ULONG AddRef()
        {
        return InterlockedIncrement(&m_cRef);
        }

        ULONG Release()
        {
        ULONG cRef = InterlockedIncrement(&m_cRef);
        if(cRef == 0)
        {
        delete this;
        return 0;
        }
        else
        return m_cRef;
        }

        //...
        };

        Possibly this isn't a complete implementation of Release(); but shouldn't m_cRef be decremented somewhere in Release();? I can't see how the InterlockedIncrement(); could do this because it is being called in AddRef(); as well as in the Release(); method. -Nick Parker

        A Offline
        A Offline
        Aravinthan 0
        wrote on last edited by
        #3

        I think it might be a typo mistake.. i feel it should be interlockeddecrement() function called instead of the increment!! for otherwise.. the component will never die..! or the cref==0 will never happen! as each time the value will always increase! Aravinthan


        Visit me:
        http://www2.domaindlx.com/earavi/
        When you know something.. its meant to share with others :-) for otherwise that knowledge has no worth:-)
        mail me:
        aravinthan@rediffmail.com


        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