描述:
void CEDlg::OnButton1()
{
// TODO: Add your control notification handler code here
IXMLDOMDocumentPtr pXmlDoc = NULL;
IXMLDOMNodeListPtr NodeListPtr = NULL;
MSXML2::IXMLDOMNode *DOMNode = NULL;
CString strContest;
HRESULT hr;
_variant_t tt("D:\\test.xml");
try
{
CoInitialize(NULL);
hr = CoCreateInstance (CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IXMLDOMDocument,(LPVOID *)&pXmlDoc);
if ( SUCCEEDED ( hr ) )
{
pXmlDoc->load(tt);
NodeListPtr = pXmlDoc->getElementsByTagName("code");
for(int i=0; i<NodeListPtr->length; i++)
{
NodeListPtr->get_item(i, &DOMNode);
strContest = (LPCSTR)DOMNode->text;
DOMNode->Release();
DOMNode = NULL;
AfxMessageBox(strContest);
}
NodeListPtr->Release();
NodeListPtr = NULL;
pXmlDoc->Release();
pXmlDoc = NULL;
}
CoUninitialize();
}
catch(_com_error &err)
{
CString strErr = (LPCTSTR)err.Description();
AfxMessageBox(strErr);
}
catch(...)
{
AfxMessageBox("unknow error");
}
}
1。每次执行到NodeListPtr = NULL;时总是报错:User breakpoint called from code at 0x77f9193c
2。如果注释掉这行,则执行到pXmlDoc = NULL;时报错:Unhandled exception in comtest.exe:0xC0000005:Access Violation.点确定后,程序停在了
void _Release() throw()
{
if (m_pInterface != NULL) {
m_pInterface->Release();
}
}的m_pInterface->Release();处。
3。如果把上述行再注释掉,则在退出该函数void CEDlg::OnButton1()时报错:Unhandled exception in comtest.exe:0xC0000005:Access Violation.点确定后,程序还是停的位置同2。
不知道是否我的程序释放资源部分不完整还是顺序有问题,请大家帮忙!
解决方案1:
同意楼上说的
解决方案2: 把
NodeListPtr->Release();
pXmlDoc->Release();
两行注释掉。COM智能指针能够通过置空的方式自己释放。