-- 作者:wondercall
-- 发布时间:4/13/2005 11:18:00 AM
-- [求助] xml读写时候内存释放问题
小弟写了一个类,使用MSXML4.dll来操作xml文件,但是在完成我想完成的操作后,点cancel按钮关闭对话框时,windows报错,经检查,发现是在l该类的析构函数处出现内存访问错误。 我把这个类贴出来,把报错内容贴出来,那位大牛给俺瞅瞅,要崩溃了都T_T //-----------------------------------------------头文件------------------------------------------------------- // XmlAccess.h: interface for the CXmlAccess class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_XMLACCESS_H__CB244AB8_79D3_43A6_BE98_E0A642D182C3__INCLUDED_) #define AFX_XMLACCESS_H__CB244AB8_79D3_43A6_BE98_E0A642D182C3__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "stdafx.h" #import "msxml4.dll" #include <atlbase.h> // Includes CComVariant and CComBSTR. class CXmlAccess { public: CXmlAccess(); virtual ~CXmlAccess(); HRESULT XmlFileOperateInit(CString strFileName); void SaveFile(CString strFileName); void release(); HRESULT getElementValue (CComBSTR bstrNodeName,CComVariant &varNodeValue); HRESULT setElementValue (CComBSTR bstrNodeName ,CComVariant varNodeValue); protected: private: MSXML2::IXMLDOMElementPtr rootNode; MSXML2::IXMLDOMDocumentPtr pDoc; }; #endif // !defined(AFX_XMLACCESS_H__CB244AB8_79D3_43A6_BE98_E0A642D182C3__INCLUDED_) //--------------------------------------------。cpp----------------------------------------------------------- // XmlAccess.cpp: implementation of the CXmlAccess class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "CXmlFileClassTry.h" #include "XmlAccess.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CXmlAccess::CXmlAccess() { } CXmlAccess::~CXmlAccess() { //rootNode->Release(); //pDoc->Release(); } //文件读写的初始化,获取文件指针和根节点以备下面的函数使用 HRESULT CXmlAccess::XmlFileOperateInit(CString strFileName) { HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30)); if(!SUCCEEDED(hr)) { // MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!"); } //加载文件 pDoc->load((CComVariant)strFileName); //获取根节点 pDoc->get_documentElement(&rootNode); // rootNode->Release(); // pDoc->Release(); // return 0; return hr; } HRESULT CXmlAccess::getElementValue (CComBSTR bstrNodeName,CComVariant &varNodeValue) { MSXML2::IXMLDOMElementPtr pXMLNode2=NULL; // MSXML2::IXMLDOMNodeListPtr nodeList2;//nodeList2 二级节点,nodeLsit3三级节点 MSXML2::IXMLDOMNodeListPtr pXMLNodeList; pXMLNodeList= rootNode->getElementsByTagName((BSTR)bstrNodeName); //if (FAILED(hr)) MessageBox("get_nodeTypedValue"); // BSTR var; // VARIANT varVal; // long nCount ; long lCount2; // Retrieve total number of nodes in the list. HRESULT hr = pXMLNodeList->get_length(&lCount2); if (lCount2<1) { //MessageBox("notfound"); } else{ // MessageBox("found,Name And Attribute Followed Next"); for (int i = 0; i < lCount2; i++) { pXMLNode2 = pXMLNodeList->nextNode(); // VARIANT varValue; hr= pXMLNode2 ->get_nodeTypedValue(&varNodeValue); if (!FAILED(hr)) { return 0; } else return 1; } //end for i } //endif //release // pXMLNode2->Release(); // nodeList2->Release(); //pXMLNodeList->Release(); } HRESULT CXmlAccess::setElementValue (CComBSTR bstrNodeName ,CComVariant varNodeValue) { BOOL replaced=0; MSXML2::IXMLDOMElementPtr pXMLNode2=NULL; // MSXML2::IXMLDOMNodeListPtr nodeList2;//nodeList2 二级节点,nodeLsit3三级节点 MSXML2::IXMLDOMNodeListPtr pXMLNodeList; pXMLNodeList= rootNode->getElementsByTagName((BSTR)bstrNodeName); // BSTR var; // VARIANT varVal; // long nCount ; long lCount2; // Retrieve total number of nodes in the list. HRESULT hr = pXMLNodeList->get_length(&lCount2); if (lCount2<1) { } else { for (int i = 0; i < lCount2; i++) { pXMLNode2 = pXMLNodeList->nextNode(); //修改节点值 hr = pXMLNode2 ->put_nodeTypedValue(varNodeValue); if (FAILED(hr)) replaced=0 ; else replaced=1; } //end for i } //end else //pXMLNode2->Release(); // nodeList2->Release();// //pXMLNodeList->Release(); return replaced; } void CXmlAccess::SaveFile(CString strFileName) { pDoc->save((CComVariant)strFileName); // pDoc->Release; // rootNode->Release; } void CXmlAccess::release() { if(rootNode!=NULL) rootNode->Release(); if(pDoc!=NULL) pDoc->Release(); } //---------------------------------------------------错误--------------------------------------------------- First-chance exception in CXmlFileClassTry.exe: 0xC0000005: Access Violation. //-----------------------------------下面是comip.h文件里的错误 // Releases only if the interface is not null. // The interface is not set to NULL. // void _Release() throw() { if (m_pInterface != NULL) { //·¥¥¥¥¥¥¥¥¥这里出错 错误-------------〉〉 m_pInterface->Release(); //·¥¥¥¥¥¥¥¥¥这里出错 } } 求救阿
|