title: Why are all references to my IDispatch object not released?
body: <p>I'm hosting an IWebBrowser2 control in my C++ program using nothing but plain Win32 (no mfc, atl, wtl etc). On DISPID_NAVIGATECOMPLETE2 I add a custom object to be accessed from javascript running on the displayed webpage.</p>
  
  <p>To add the custom object I call InvokeEx with DISPATCH_PROPERTYPUT and a DISPPARAMS structure with a pointer to my custom object.</p>
  
  <p>During the call to InvokeEx the AddRef function of my custom object is called and I increment its reference counter. But the object never gets a call to its Release function, so the reference counter never decreases down to zero again.</p>
  
  <p>Who is responsible for calling Release() after AddRef() has been called in InvokeEx?</p>
  
  <p><strong>EDIT:</strong> (Adding some code)</p>
  
  <p>This is the code that adds my custom object to the IHTMLWindow2. <code>custObj</code> points to my custom object</p>
  
  <pre><code>IHTMLWindow2 *win = NULL;
  GetDoc()-&gt;get_parentWindow(&amp;win);
  IDispatchEx *winEx = NULL;
  win-&gt;QueryInterface(&amp;winEx);
  
  DISPID dispid;
  HRESULT hr = winEx-&gt;GetDispID(objName, fdexNameEnsure, &amp;dispid); //objName is "JSObject"
  
  DISPID namedArgs[] = {DISPID_PROPERTYPUT};
  DISPPARAMS params;
  params.rgvarg = new VARIANT[1];
  params.rgvarg[0].pdispVal = custObj;
  params.rgvarg[0].vt = VT_DISPATCH;
  params.rgdispidNamedArgs = namedArgs;
  params.cArgs = 1;
  params.cNamedArgs = 1;
  
  hr = winEx-&gt;InvokeEx(dispid, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT, &amp;params, NULL, NULL, NULL); 
  </code></pre>
  
  <p>This is the object that I'm adding (some private members have been left out for brevity)</p>
  
  <pre><code>class JSObject : public IDispatch {
  private:
      long ref;
  public:
      JSObject();
  
      // IUnknown
      virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppv);
      virtual ULONG STDMETHODCALLTYPE AddRef();
      virtual ULONG STDMETHODCALLTYPE Release();
  
      // IDispatch
      virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(UINT *pctinfo);
      virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(UINT iTInfo, LCID lcid,
          ITypeInfo **ppTInfo);
      virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames(REFIID riid,
          LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId);
      virtual HRESULT STDMETHODCALLTYPE Invoke(DISPID dispIdMember, REFIID riid,
          LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
          EXCEPINFO *pExcepInfo, UINT *puArgErr);
  };
  </code></pre>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Now that it seems to be working like it should I decided to put it on github.</p>
  
  <p><a href="https://github.com/Tobbe/CppIEEmbed" rel="nofollow">https://github.com/Tobbe/CppIEEmbed</a>. Please fork and improve if you can :)</p>