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. A Juicy COM Bug

A Juicy COM Bug

Scheduled Pinned Locked Moved COM
c++helpphpcomdebugging
2 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.
  • M Offline
    M Offline
    mango_lier
    wrote on last edited by
    #1

    There is an article on codeguru (http://www.codeguru.com/Cpp/misc/misc/article.php/c6093/[^]) about using active scripting with MFC or ATL/WTL application. The code library instruments connection points between VBScript and host application so one can call C/C++ functions from VBScript and share the variables between the two. The implementation is reasonable except one issue, It suffer from an “Interface Leak”. One can see the leak running the sample program with the article in debug mode. It seems the AddRef()/Release() are not balanced in the code. I was curios to see where the bug was. Tracing the interface code, it seems that “QueryInterface()” is seemingly adding implicit reference on the interface. I fixed the problem by inserting an extra Release() on pIDisp on line 308 in file “ScriptHost.cpp” after the AddRef(). It seemingly solves the problem with no more interface leaks. The fix above is for function residing in the application, the same fix on couple of lines above solves the shared variable issue too. I am curious to get some input from COM gurus why a call to QueryInterface() is adding an extra reference on the interface.

    J 1 Reply Last reply
    0
    • M mango_lier

      There is an article on codeguru (http://www.codeguru.com/Cpp/misc/misc/article.php/c6093/[^]) about using active scripting with MFC or ATL/WTL application. The code library instruments connection points between VBScript and host application so one can call C/C++ functions from VBScript and share the variables between the two. The implementation is reasonable except one issue, It suffer from an “Interface Leak”. One can see the leak running the sample program with the article in debug mode. It seems the AddRef()/Release() are not balanced in the code. I was curios to see where the bug was. Tracing the interface code, it seems that “QueryInterface()” is seemingly adding implicit reference on the interface. I fixed the problem by inserting an extra Release() on pIDisp on line 308 in file “ScriptHost.cpp” after the AddRef(). It seemingly solves the problem with no more interface leaks. The fix above is for function residing in the application, the same fix on couple of lines above solves the shared variable issue too. I am curious to get some input from COM gurus why a call to QueryInterface() is adding an extra reference on the interface.

      J Offline
      J Offline
      Jorgen Sigvardsson
      wrote on last edited by
      #2

      mango_lier wrote: I am curious to get some input from COM gurus why a call to QueryInterface() is adding an extra reference on the interface. The COM rules says explicitly that a successful QueryInterface shall increase the reference count. This must be the case if you think about it. You are getting a reference to a new interface handle. It must be AddRef()'d because of situations like these:

      IUnknown* pUnk = ...; // refcount is 1 after this line
      IMyInterface* pItf;
      pUnk->QueryInterface(__uuidof(IMyInterface), (void**)&pItf);
      pUnk->Release();
      pItf->MyMethod();

      If QueryInterface didn't AddRef(), then the code above would be invalid. The COM rules assures that client code does not need to be complex. Good music: In my rosary[^] -- modified at 5:20 Sunday 25th September, 2005

      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