CComPtr 销毁期间出现异常

发布于 2024-09-10 17:47:48 字数 597 浏览 9 评论 0原文

我有一个声明为

CComPtr<IXMLDOMDocument2> m_spXMLDoc;

XML 文档的成员变量是这样创建的

CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
    IID_IXMLDOMDocument2, (void**)&m_spXMLDoc));

现在当应用程序退出时,会抛出异常。 Callstack 指向 p->Release()

~CComPtrBase() throw()
{
   if (p)
      p->Release();
}

当我将鼠标悬停在 VS 调试器中的 p 时,它指向一些有效的内存。

最后一个调用堆栈指向 msxm6 中的异常

msxml6.dll!3d6cXX03() 

有什么建议,可能是什么原因?我不认为这是一个 CComPtr 问题。

I have a member variable declared as

CComPtr<IXMLDOMDocument2> m_spXMLDoc;

XML document is created like this

CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
    IID_IXMLDOMDocument2, (void**)&m_spXMLDoc));

Now when application exits, an exception is thrown. Callstack is pointing to p->Release()

~CComPtrBase() throw()
{
   if (p)
      p->Release();
}

When I hover over to p in VS debugger, it points to some valid memory.

The last callstack points to exception in msxm6

msxml6.dll!3d6cXX03() 

Any suggestions, what could be the reason? I don't think it's a CComPtr issue.

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

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

发布评论

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

评论(4

黯然 2024-09-17 17:47:48

我遇到了类似的问题,最终我发现这只是一个错误。我必须确保在破坏 CComPtr 之后调用 CoUninitialize() 。否则,就会出现异常。

int _tmain(int argc, _TCHAR* argv[]) {
  CoInitialize(NULL);
  mymain(); 
  //put all logic in a separate function so that CComPtr
  //is destructed before CoUninitialize()
  CoUninitialize();
  return 0;
}

在与 CoUninitialize() 调用相同的函数中声明 CComPtr 将导致异常,因为销毁发生在函数终止后。

I had a similar issue and eventually I found that it is was just a bug. I have to make sure that CoUninitialize() is called AFTER the CComPtr is destructed. Otherwise, there will be an exception.

int _tmain(int argc, _TCHAR* argv[]) {
  CoInitialize(NULL);
  mymain(); 
  //put all logic in a separate function so that CComPtr
  //is destructed before CoUninitialize()
  CoUninitialize();
  return 0;
}

Declaring CComPtr in the same function as the CoUninitialize() call will cause the exception since the destruction occurs after the function terminates.

紫南 2024-09-17 17:47:48

在程序退出之前执行此操作:

if( m_spXMLDoc.p )
    m_spXMLDoc.Release();

我之前就见过这种情况。这个问题与引用计数有关(显然),但我从来没有想过寻找原因。希望这有帮助!

Do this before your program exits:

if( m_spXMLDoc.p )
    m_spXMLDoc.Release();

I've seen this before myself. The issue is related with reference counting (obviously), but I've never cared to look for the reason. Hope this helps!

够钟 2024-09-17 17:47:48

您应该使用 CComPtr 的成员函数创建实例:

m_spXMLDoc.CoCreateInstance(...)

You should create the instance using the member functions of CComPtr:

m_spXMLDoc.CoCreateInstance(...)
月下伊人醉 2024-09-17 17:47:48

我正在研究一个类似的问题,其中 IExplorer 从客户端下抓取当前网页的 com 服务器。
结果似乎无法执行发布,而是出现诸如服务器已断开客户端连接之类的 com 错误。

I'm looking at a similar issue where IExplorer rips the com server for the current web page from under the clients.
The result seems to be that release can't be performed, instead you get com errors like server has disconnected clients.

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