sanjay soni
September 9th, 2003, 02:56 AM
Hi,
Following piece of code generates lots of memory leaks and
I am not able to remove them. Can anybody help me out what
is wrong with the following piece of code and why does it
have memory leaks?
Well just for the brief, this code given the filelist,
which are lying on the harddisk(.xml files), picks up
those files from harddisk, load them and store them in
xmlStr. And this xmlStr is later on used for other
purposes.
bool GenerateAccountingXML(vector<tstring> *fileList, BSTR
* xmlStr)
{
HRESULT hr;
ULONG refcount=0;
IXMLDOMDocument *pXMLDoc = NULL;
IXMLDOMDocument *pXMLBatchDoc = NULL;
IXMLDOMNode *childNode = NULL;
IXMLDOMNode *newNode = NULL;
IXMLDOMElement* pRoot= NULL;
IXMLDOMParseError *pParserError = NULL;
_variant_t fileName;
VARIANT_BOOL ret;
static bool logOnce=false;
// generate XML
hr = CoCreateInstance(CLSID_DOMDocument, NULL,
CLSCTX_INPROC_SERVER,IID_IXMLDOMDocument, (void**)
&pXMLDoc);
if(FAILED(hr))
{
if(!logOnce)
{
logOnce=true;
LogMsg(_T("Cannot create Instance
of XMLDOMDocument. Error code=> %d"), hr,
EVENTLOG_ERROR_TYPE);
}
return false;
}
hr = CoCreateInstance(CLSID_DOMDocument, NULL,
CLSCTX_INPROC_SERVER,IID_IXMLDOMDocument, (void**)
&pXMLBatchDoc);
if(FAILED(hr))
{
if(!logOnce)
{
logOnce=true;
LogMsg(_T("Cannot create Instance
of XMLDOMDocument. Error code=> %d"), hr,
EVENTLOG_ERROR_TYPE);
}
return false;
}
pXMLBatchDoc->createElement(BSTR
(L"AccountingData") , &pRoot );
for (unsigned int i=0;i < fileList->size();i++)
{
#ifdef _UNICODE
fileName=(fileList->at(i)).c_str();
#else
fileName.SetString(((tstring)fileList->at
(i)).c_str());
#endif
pXMLDoc->put_async(false);
if(pXMLDoc->load(fileName,&ret) == S_FALSE)
LogMsg(_T("Unable to load
XML file for sending to the server, because it has the
invalid format"),NULL,EVENTLOG_ERROR_TYPE);
else
{
pXMLDoc->get_firstChild
(&childNode);
pRoot->appendChild
(childNode,&newNode);
}
}
pXMLBatchDoc->appendChild(pRoot,&newNode);
pXMLBatchDoc->get_xml(xmlStr);
// do the clean up
refcount = newNode->Release();
refcount = pRoot->Release();
refcount = pXMLDoc->Release();
refcount = pXMLBatchDoc->Release();
// turn back logOnce to false so that next time we
can log the new error again.
logOnce=false;
return true;
}
Following piece of code generates lots of memory leaks and
I am not able to remove them. Can anybody help me out what
is wrong with the following piece of code and why does it
have memory leaks?
Well just for the brief, this code given the filelist,
which are lying on the harddisk(.xml files), picks up
those files from harddisk, load them and store them in
xmlStr. And this xmlStr is later on used for other
purposes.
bool GenerateAccountingXML(vector<tstring> *fileList, BSTR
* xmlStr)
{
HRESULT hr;
ULONG refcount=0;
IXMLDOMDocument *pXMLDoc = NULL;
IXMLDOMDocument *pXMLBatchDoc = NULL;
IXMLDOMNode *childNode = NULL;
IXMLDOMNode *newNode = NULL;
IXMLDOMElement* pRoot= NULL;
IXMLDOMParseError *pParserError = NULL;
_variant_t fileName;
VARIANT_BOOL ret;
static bool logOnce=false;
// generate XML
hr = CoCreateInstance(CLSID_DOMDocument, NULL,
CLSCTX_INPROC_SERVER,IID_IXMLDOMDocument, (void**)
&pXMLDoc);
if(FAILED(hr))
{
if(!logOnce)
{
logOnce=true;
LogMsg(_T("Cannot create Instance
of XMLDOMDocument. Error code=> %d"), hr,
EVENTLOG_ERROR_TYPE);
}
return false;
}
hr = CoCreateInstance(CLSID_DOMDocument, NULL,
CLSCTX_INPROC_SERVER,IID_IXMLDOMDocument, (void**)
&pXMLBatchDoc);
if(FAILED(hr))
{
if(!logOnce)
{
logOnce=true;
LogMsg(_T("Cannot create Instance
of XMLDOMDocument. Error code=> %d"), hr,
EVENTLOG_ERROR_TYPE);
}
return false;
}
pXMLBatchDoc->createElement(BSTR
(L"AccountingData") , &pRoot );
for (unsigned int i=0;i < fileList->size();i++)
{
#ifdef _UNICODE
fileName=(fileList->at(i)).c_str();
#else
fileName.SetString(((tstring)fileList->at
(i)).c_str());
#endif
pXMLDoc->put_async(false);
if(pXMLDoc->load(fileName,&ret) == S_FALSE)
LogMsg(_T("Unable to load
XML file for sending to the server, because it has the
invalid format"),NULL,EVENTLOG_ERROR_TYPE);
else
{
pXMLDoc->get_firstChild
(&childNode);
pRoot->appendChild
(childNode,&newNode);
}
}
pXMLBatchDoc->appendChild(pRoot,&newNode);
pXMLBatchDoc->get_xml(xmlStr);
// do the clean up
refcount = newNode->Release();
refcount = pRoot->Release();
refcount = pXMLDoc->Release();
refcount = pXMLBatchDoc->Release();
// turn back logOnce to false so that next time we
can log the new error again.
logOnce=false;
return true;
}