为什么无法调用COM对象的Release接口

发布于 2024-09-26 11:25:29 字数 1498 浏览 0 评论 0原文

我正在为 VC6 和 VC9 开发一个 VC 插件。以下代码均来自我的作品。在 CViaDevStudio::Evaluate 中,在我调用 pDebugger->Release() 后一切正常。但在CViaVisualStudio::ReadFromMemory中,当我调用pDebugger->Release()pProc->Release()后,VC9会提示错误投诉 未指定的错误号。我不知道为什么。我认为在使用 COM 对象后调用 Release() 是合理的。

/* VC6 */
class CViaDevStudio {
...
IApplication* m_pApplication;
};

BOOL    CViaDevStudio::Evaluate(char* szExp, TCHAR* value, int size)
{
    BOOL re = FALSE;
    IDebugger*  pDebugger = NULL;
    m_pApplication->get_Debugger((IDispatch**)&pDebugger);

    if (pDebugger) {
        ...
    }

exit:
        // The following code must be called, otherwise VC6 will complaint invalid access
        // when it's started again
    if (pDebugger)
        pDebugger->Release();

    return re;
}    

/* VC9 */
class     CViaVisualStudio {
    ...
    CComPtr<EnvDTE::_DTE> m_pApplication;
};

BOOL    CViaVisualStudio::ReadFromMemory(PBYTE pDst, PBYTE pSrc, long size)
{
    BOOL re = FALSE;
    EnvDTE::DebuggerPtr pDebugger;
    EnvDTE::ProcessPtr pProc;

    if (S_OK == m_pApplication->get_Debugger(&pDebugger)) {
        if (S_OK == pDebugger->get_CurrentProcess(&pProc)) {
            ...
            }
        }
    }

exit:
    // I don't know why the following enclosed with macros should not be called.
#if 0
    if (pDebugger)
        pDebugger->Release();
    if (pProc)
        pProc->Release();
#endif
    return re;
}

I'm develop a VC Add-In for VC6 and VC9. The following codes are from my works. In CViaDevStudio::Evaluate, after I call pDebugger->Release() and it's all OK. But in CViaVisualStudio::ReadFromMemory, after I call pDebugger->Release() or pProc->Release(), VC9 will prompt a error complaint a unspecified error number. I don't know why. I think it's reasonable to call Release() after I have used a COM object.

/* VC6 */
class CViaDevStudio {
...
IApplication* m_pApplication;
};

BOOL    CViaDevStudio::Evaluate(char* szExp, TCHAR* value, int size)
{
    BOOL re = FALSE;
    IDebugger*  pDebugger = NULL;
    m_pApplication->get_Debugger((IDispatch**)&pDebugger);

    if (pDebugger) {
        ...
    }

exit:
        // The following code must be called, otherwise VC6 will complaint invalid access
        // when it's started again
    if (pDebugger)
        pDebugger->Release();

    return re;
}    

/* VC9 */
class     CViaVisualStudio {
    ...
    CComPtr<EnvDTE::_DTE> m_pApplication;
};

BOOL    CViaVisualStudio::ReadFromMemory(PBYTE pDst, PBYTE pSrc, long size)
{
    BOOL re = FALSE;
    EnvDTE::DebuggerPtr pDebugger;
    EnvDTE::ProcessPtr pProc;

    if (S_OK == m_pApplication->get_Debugger(&pDebugger)) {
        if (S_OK == pDebugger->get_CurrentProcess(&pProc)) {
            ...
            }
        }
    }

exit:
    // I don't know why the following enclosed with macros should not be called.
#if 0
    if (pDebugger)
        pDebugger->Release();
    if (pProc)
        pProc->Release();
#endif
    return re;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦÷微暖 2024-10-03 11:25:29
EnvDTE::DebuggerPtr pDebugger;

请注意指针的声明与 Evaluate 中的声明方式有何不同。它是一个 IDebugger* 。 DebuggerPtr 类是由#import 指令生成的包装类。它是一个智能指针类,它知道如何自动调用Release()。即使代码抛出异常,也不会泄漏指针。强烈推荐。您会发现它在 MSDN 库中记录为 _com_ptr_t 类。

EnvDTE::DebuggerPtr pDebugger;

Note how the declaration of the pointer is different from the way you did it in Evaluate. It is an IDebugger* there. The DebuggerPtr class is a wrapper class generated by the #import directive. It is a smart pointer class, it knows how to call Release() automatically. Even if the code throws exceptions, it won't leak the pointer. Highly recommended. You'll find it documented in the MSDN library as the _com_ptr_t class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文