0

I am using below code to manipulate visual studio.

Everything worked fine except that smart pointers did not release properly before function exit: it threw exception upon release.:exception 0xc0000005, access violation when write to 0xfeeefeee.

the running stop at the bold part below.

public:
    typedef T _PtrClass;
    ~CComPtrBase() throw()
    {
        if (p)
            p->Release();
    }

Could any one give me some hint?

int myAutomateVS()
{

    CoInitialize(NULL);

    CComPtr<IRunningObjectTable> pTable=NULL;
    GetRunningObjectTable(0, &pTable);

    CComPtr<IEnumMoniker> pEnumMoniker=NULL;;
    pTable->EnumRunning(&pEnumMoniker);

    CComPtr<IMoniker> pMoniker=NULL;
    ULONG nMoniker=0;

    CComPtr<IUnknown> pRunningObj=NULL;
    while(pEnumMoniker->Next(1, &pMoniker, &nMoniker) == S_OK)
    {
        CComPtr<IBindCtx> pCtx=NULL;
        CreateBindCtx(0, &pCtx);

        LPOLESTR pwszName;
        pMoniker->GetDisplayName(pCtx, NULL, &pwszName);


        pTable->GetObject(pMoniker, &pRunningObj);

        if(_tcsstr(pwszName, L"VisualStudio.DTE.8") != NULL)
            break;
    }
    CComPtr<EnvDTE::_DTE> pDTE;
    pDTE = pRunningObj;

    CComPtr<EnvDTE::_Solution> solution;
    pDTE->get_Solution(&solution);
    CComBSTR fullName;
    solution->get_FullName(&fullName);

    CoUninitialize();
    return 0;
}
7
  • which exception is thrown?
    – sergico
    Commented Sep 13, 2015 at 14:45
  • the exception was: 0xc0000005, access violation writing to 0xfeeefeee.
    – Gang Li
    Commented Sep 13, 2015 at 14:54
  • 1
    Where are your copy constructors and assignment operators?
    – timrau
    Commented Sep 13, 2015 at 15:01
  • 1
    You fell victim to one of the classic blunders - you call CoUninitialize while CComPtr objects holding interface pointers are still in scope. They will call Release() on their underlying dumb pointers at the closing brace - but that's illegal after CoUninitialize Commented Sep 13, 2015 at 15:11
  • pTable->GetObject(pMoniker, &pRunningObj); This leaks - the interface pointer currently held by pRunningObj is overwritten and never released. Commented Sep 13, 2015 at 15:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.