Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am facing a memory leak issue while parsing a XML document using MSXML.
Memory in task manager is increasing continuously and finally the application crashes.

Please find the below code snippet for same.
C++
void ValidateName(MSXML::IXMLDOMNodePtr pNode,CString sName,CString sController, bool& bRecurse, bool bControllerMatchFound)
{
	
	USES_CONVERSION;

	if (NULL == pNode)
		return  ;

	try
	{
		pNode->AddRef();
		// Recurse Children
		if (bRecurse)
		{
			
			CString strText;
			int nImageToAdd;
			int nSelectedImageToAdd;
			strText = NodeToTextImage(pNode, nImageToAdd, nSelectedImageToAdd);
			pNode->AddRef();

			m_pCurNode = pNode;
			MSXML::IXMLDOMNodePtr pParentNode	= m_pCurNode->GetparentNode();
			MSXML::IXMLDOMNamedNodeMapPtr pAttrList = pParentNode->Getattributes();
			if (NULL != pAttrList)				
			{
				
				MSXML::IXMLDOMAttributePtr pAttr;
				while (pAttr = pAttrList->nextNode())
				{
					CString strNodeName = W2T(pAttr->GetnodeName());
					CString strNodeAttribute;
					if ((strNodeName.CompareNoCase(_T("tag")) == 0) || (strNodeName.CompareNoCase(_T("id")) == 0) )
					{
						strNodeAttribute = W2T(_bstr_t(pAttr->GetnodeValue()));
						m_Info.SetParent(strNodeAttribute);
						break;
					}
				

				}
			}
			bRecurse = false;
			break;				
			MSXML::IXMLDOMNodeListPtr pNodeList = pNode->GetchildNodes();
			if (pNodeList != NULL)
			{
				MSXML::IXMLDOMNodePtr pChild;
				while(pChild = pNodeList->nextNode())
				{
					ValidateName(pChild,sName,sController, bRecurse,bControllerMatchFound);
				}
			}
		}
	}
	catch(_com_error e)
	{
		TRACE(_T("Caught Exception: FillXMLTree"));
		//ReportError(e, _T("tree scan"));
	}
	catch(...)
	{
		TRACE(_T("Caught Exception: FillXMLTree"));
		AfxMessageBox(_T("Error filling tree!"));
	}
	
}
The XML document contains many nodes in it. I am searching for a particular node element under it. But till completing execution of this function memory usage increases for each search.
I am using msxml.dll.

Is there anything wrong in this code or is there any mechanism by which I can free some memory during parsing.

Help of any kind will be highly appreciated.

Thank you.
Posted
Updated 17-Oct-13 21:40pm
v3
Comments
nv3 18-Oct-13 11:57am    
As far as I understand your code, you only recurse to level 1, as you set bRecurse to false when processing the list of children. So it should be relatively easy to step through the code with a debugger.

You seem to have left code parts out; otherwise I can't see what the USES_CONVERSION should be good for that you do at the beginning.

1 solution

For the first the IXMLDOMNodePtr and IXMLDOMNamedNodeMapPtr etc. are smart pointers. they must not been AddRef'ed and not Release'd. If you call the AddRef you MUST call a Release to the same interface pointer! In your code are two AddRef calls and no Release. Thats the reason the parameter pNode is never Released.

On the position:
m_pCurNode = pNode;

I don't know what type
m_pCurNode
is but I assume its a smart pointer too. so you have only to remove the two
pNode->AddRef();
expressions from your code.

regards.
 
Share this answer
 
Comments
cooltabrej 22-Oct-13 6:10am    
Yes you are right. Your suggestion worked for me. When i commented addref() calls from code memory leakage has stopped. :)

Can you please provide me some links where i can find more information about use of addref() and release() with smart pointers.

Thank you very much. :)

Regards,
cooltabrej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900