Hi, In my application I used xerces for XML parsing. Here is my code . Is shows memory leak of 4K each time this code is executed. Which memory should I free?
int CCommandInfo::XMLToObject(TCHAR *XMLBuff) { int iRetVal = -1; try { if (NULL == XMLBuff) { return iRetVal; } // Create DOM Parser XercesDOMParser parser; parser.setValidationScheme(XercesDOMParser::Val_Always); parser.setDoNamespaces(true); wstring wsBuff = XMLBuff; string strBuff(wsBuff.begin(), wsBuff.end()); MemBufInputSource xmlBuf((XMLByte *)strBuff.c_str(), strBuff.length(), L"108"); parser.parse(xmlBuf); // Fetch the DOM Document from parser xercesc_2_8::DOMDocument *pDomDoc = parser.getDocument (); if (pDomDoc == NULL) { LogError( L"Invalid XML document"); goto CLEANUP; } DOMElement *pCommandInfoNode = pDomDoc->getDocumentElement (); if(NULL == pCommandInfoNode) { LogError( L"Invalid XML document element"); goto CLEANUP; } DOMNodeList *pChildNodes = pCommandInfoNode->getChildNodes(); if(NULL == pChildNodes) { LogError( L"Invalid XML child nodes"); goto CLEANUP; } XMLSize_t childCnt = pChildNodes->getLength(); for (int i = 0; i < (int) childCnt; i++) { XMLCh *nodeName = (XMLCh *)pChildNodes->item(i)->getNodeName(); if (!nodeName) { LogError( L"Could not fetch Node Name"); continue; } // only check for request not for responce as it shold and must be empty if(XMLString::compareString(nodeName, REQUEST) == 0) { DOMElement *pReqElem = (DOMElement *) pChildNodes->item(i); int iRet = m_Request.XMLToObject(pReqElem); if (iRet != ERROR_SUCCESS) { LogError( L"Invalid XML CommandInfo"); goto CLEANUP; } break; } } iRetVal = ERROR_SUCCESS; CLEANUP: ; } catch (const DOMException& toCatch) { char* message = XMLString::transcode(toCatch.msg); XMLString::release(&message); return iRetVal; } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); XMLString::release(&message); return iRetVal; } catch (...) { LogError( L"Unhandled exception occured in XMLToObject() of CCommandInfo class.\n"); return iRetVal; } return iRetVal; } /* Xerces specific memory managment=> see http://xerces.apache.org/xerces-c/program-dom-3.html#UsingDOMAPI for memory handling http://xerces.apache.org/xerces-c/ApacheDOMC++Binding.html#release C++ Memory Management: A member method "release()" is added for releasing any "orphaned" resources that were created through createXXXX factory method. Memory for any returned object e.g. DOMImplementation* getDOMImplementation(const XMLCh* features) ... etc. are owned by implementation Xerces specific end caller has to release memory in *XMLBuff */ int CCommandInfo::ObjectToXML(TCHAR **XMLBuff) { int iRetVal = -1; try { MemBufFormatTarget myMem; xercesc_2_8::DOMDocument *pDomDoc = NULL; DOMWriter *pDomSerializer = NULL; DOMImplementation *pDomImpl = DOMImplementationRegistry::getDOMImplementation(NULL); if (NULL == pDomImpl) { LogError( L"Requested implementation is not supported"); goto CLEANUP; } pDomDoc = pDomImpl->createDocument( NULL, // root element namespace URI. COMMANDINFO, // root element name NULL); // document type object (DTD). if (NULL == pDomDoc) { LogError( ERROR_CREATE_DOM); goto CLEANUP; } // Create the DOM Writer object to write the XML pDomSerializer = ((DOMImplementationLS *)pDomImpl)->createDOMWriter(); if (NULL == pDomSerializer) { goto CLEANUP; } // Get the pointer to root element (CommandInfo) DOMElement *pCmdInfoElem = pDomDoc->getDocumentElement(); if (NULL == pCmdInfoElem) { goto CLEANUP; } // We are sending back the response,but as we created new document // we need to add both request and responce int iRet = m_Request.ObjectToXML(pCmdInfoElem, pDomDoc); if (iRet != ERROR_SUCCESS) { goto CLEANUP; } iRet = m_Response.ObjectToXML(pCmdInfoElem, pDomDoc); if (iRet != ERROR_SUCCESS) { goto CLEANUP; } MemBufFormatTarget *myMemory = &myMem; if (NULL == myMemory) { goto CLEANUP; } if (false == pDomSerializer->writeNode(myMemory, *pDomDoc)) { // Failed to serialize the XML goto CLEANUP; } char *outXmlBuf = NULL; outXmlBuf = (char *) myMemory->getRawBuffer(); if (NULL == outXmlBuf) { goto CLEANUP; } int iLen = myMemory->getLen(); (*XMLBuff) = new TCHAR[iLen+1]; iRet = CHAR2TCHAR(outXmlBuf,XMLBuff); if (NULL == *XMLBuff) { goto CLEANUP; } LOGXMLBUFFALLOCATION(*XMLBuff) // (*XMLBuff) is freed by caller iRetVal = ERROR_SUCCESS; CLEANUP: if(NULL != pDomSerializer) { pDomSerializer->release(); } if(NULL != pDomDoc) { pDomDoc->release(); } } catch (const DOMException& toCatch) { char* message = XMLString::transcode(toCatch.msg); XMLString::release(&message); return iRetVal; } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); XMLString::release(&message); return iRetVal; } catch (...) { LogError( L"Unhandled exception occured in ObjectToXML() of CCommandInfo class."); return iRetVal; } return iRetVal; } -- View this message in context: http://old.nabble.com/How-to-delete-memory-allocated-by-xerces-objects-tp31819296p31819296.html Sent from the Xerces - C - Users mailing list archive at Nabble.com.