2010-10-07 77 views
0

我正在开发用于VC6和VC9的VC加载项。以下代码来自我的作品。在CViaDevStudio::Evaluate之后,我打电话给pDebugger->Release(),一切正常。但在CViaVisualStudio::ReadFromMemory中,在我拨打pDebugger->Release()pProc->Release()后,VC9会提示错误提示错误号码未指定。我不知道为什么。我认为在使用COM对象后调用Release()是合理的。为什么我不能调用发布COM对象的接口

/* 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; 
} 

回答

2
EnvDTE::DebuggerPtr pDebugger; 

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

+0

非常感谢! – 2010-10-08 02:17:59

相关问题