ComClient easy integration crash at shutdown

Latest post 07-10-2008 9:17 AM by Gary. 4 replies.
  • 07-08-2008 7:02 AM

    • kab
    • Top 50 Contributor
    • Joined on 09-28-2007
    • Posts 5

    ComClient easy integration crash at shutdown

    Hello

    I am new to VSTA and have downloaded the 'COMClient easy integration - using generic COMProxy and VAO' sample and am trying it out. Wow it takes a lot of complicated setup just to get VSTA and the sample running. I must admit that my knowledge of managed code is rather limited, that is why I am using the sample, and concentrating on the COM part.

    I have created a new exe (TestGUI) to replace ComClient and a new ATL COM Sever (VSTATester) which contains a COM object (HostControlTest) to replace MSDNComServer. I can launch the IDE, create a script and run it as a macro. However, there are some problems with the ref. counting on the ComConnector which often leads to a crash at shut down!

    I have tried to look at Gary's comment (http://www.summsoft.com/files/folders/vsta_samples/entry321.aspx) regarding the StopVSTA, but could unfortunately not make it help. I also tried inserting the 'CoEEShutDownCOM()' and 'CorExitProcess(0)' without much luck.

    I do the following:
    -Create the pVAO, connector etc. as in the ComClient sample
    -Run the macro using pVao->LoadMacros(true);
    -Wait for the macro to execute, it uses this.GetHostObject()
    -Release the com objects I created

    pVao->RegisterHostItem() addref's the ICOMConnector given but the refcount is decreased as the variant containing the ComConnector runs out of scope! This means that I cannot release my ComConnector pointer since this would release the com object causing a crash when the RemoteComObjectAdapter tries to access it.

    Further the 'pVao->LoadMacros(true);' call addref's the ComConnector but does not seem to release it. The macro call this->GetHostObject(). Should the macro somehow release the ComConnector?

    When I close my application I get an exception somewhere in: Microsoft.VisualStudio.Tools.Applications.InteropAdapter.dll!Microsoft.VisualStudio.Tools.Applications.ComObjectAdapter.FreeNativeResources() This is if I debug the 'VAO' project. If I debug my test client it is an unhandled exception. It happend after leaving WinMain of my application.

    I assume I am doing something wrong but have now spent many hours debugging and trying various ideas and can still not solve the problem.

    Could someone please help? Anyone seen similar problems?

    Kind regards,
    Kim
  • 07-08-2008 9:47 AM In reply to

    • Gary
    • Top 10 Contributor
    • Joined on 07-13-2006
    • Posts 293

    Re: ComClient easy integration crash at shutdown

    I think the ComConnector part has a refcount bug.  I can fix this up and send it along, but here's the part that should help:

    =====

    #include "stdafx.h"
    #include "Connector.h"
    #include "VAOCOMConnector.h"


    // CConnector
     
    // holds reference to host application object for the session

    CConnector::CConnector() : m_pdispHostApplicationObject(NULL)
    {
    }

    void CConnector::FinalRelease()
    {
     if(m_pdispHostApplicationObject)
     {
      m_pdispHostApplicationObject->Release();
     }
    }

    STDMETHODIMP CConnector::AssignHostObject(IDispatch* pdispHostObject)
    {
     if(m_pdispHostApplicationObject)
     {
      m_pdispHostApplicationObject->Release();
     }
     m_pdispHostApplicationObject = pdispHostObject;
     m_pdispHostApplicationObject->AddRef();

     return S_OK;
    }

    STDMETHODIMP CConnector::GetHostObject(IDispatch** ppdispHostObject)

     if(ppdispHostObject == NULL)
      return E_POINTER;

     *ppdispHostObject = m_pdispHostApplicationObject;
     return S_OK;
    }

  • 07-09-2008 8:06 AM In reply to

    • kab
    • Top 50 Contributor
    • Joined on 09-28-2007
    • Posts 5

    Re: ComClient easy integration crash at shutdown

    Gary,

    Thanks a lot. I agree there was a ref count bug and I think the GetHostObject method should also addref the interface since it hands out another pointer.

    However, this was not enough.

    Case 1: The macro does NOT call GetHostObj()

    The pVao->LoadMacros(true) call addref's 1 comconnector each time it is called which is never released!

    Case 2: The macro calls GetHostObj()
    The pVao->LoadMacros(true) call still addref's 1 comconnector each time it is called and also addref's the hostobj as expected. But again none of them are release when the macro is done!

    My big problem is that I cannot figure out where. And if they are not release the application crashes at shut down.

    pVao->StopVSTA alway releases 3 references to the host, but this is only correct if LoadMacro was never called.

    The pVao->ShowIDE(VARIANT_TRUE); call seems to work fine.

    Thus it still seems that the managed code tries to release the remaining references which are already released! I assume this is because it is released after I call CoUninitialize. As mentioned I have inserted the GC code from your post in StopVSTA:
        mHostItemProvider = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();

    Do you have any suggestions as to where I can release the acquired comconnector and host references from the Vao?

    Thanks again, I really appreciate you help.

    Kim
  • 07-10-2008 4:59 AM In reply to

    • kab
    • Top 50 Contributor
    • Joined on 09-28-2007
    • Posts 5

    Re: ComClient easy integration crash at shutdown

    The crash at shutdown seems to happen when exiting the RemoteComObjectAdapter finalizer with the below call stack:

         Microsoft.VisualStudio.Tools.Applications.InteropAdapter.dll!Microsoft.VisualStudio.Tools.Applications.ComObjectAdapter.FreeNativeResources() + 0x46 bytes   
         Microsoft.VisualStudio.Tools.Applications.InteropAdapter.dll!Microsoft.VisualStudio.Tools.Applications.ComObjectAdapter.!ComObjectAdapter() + 0x18 bytes   
         Microsoft.VisualStudio.Tools.Applications.InteropAdapter.dll!Microsoft.VisualStudio.Tools.Applications.ComObjectAdapter.Dispose(bool  = false) + 0x32 bytes   
         Microsoft.VisualStudio.Tools.Applications.InteropAdapter.dll!Microsoft.VisualStudio.Tools.Applications.ComObjectAdapter.Finalize() + 0x1d bytes   
         010a00e6()   
    >    VAO.RemoteComObjectAdapter.dll!VAO.RemoteComObjectAdapter.RemoteComObjectAdapter.Finalize() Line 27 + 0x20 bytes

    Kim

  • 07-10-2008 9:17 AM In reply to

    • Gary
    • Top 10 Contributor
    • Joined on 07-13-2006
    • Posts 293

    Re: ComClient easy integration crash at shutdown

    I'll get you an improved sample via vstasupport@summsoft.com
Page 1 of 1 (5 items) | RSS
Copyright Summit Software Company, 2008. All rights reserved.