Click to See Complete Forum and Search --> : Memory Leaks


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;
}

erwin_78
September 11th, 2003, 03:43 AM
if createinstance of pXMLBatchDoc fails, you do not free pXMLDoc.

and i'm not sure about REloading those xml files in DOM (already contains a xml file) without clearing the previous xml file...

sanjay soni
September 11th, 2003, 04:22 AM
erwin,

OK, let's assume that CoCreateInstance never fails in this case, because it never failed in our case. Also contents of xmlStr are always cleared before calling the GenerateAccountingXML function, hence we dont have to worry about it.

Anyway, I see the memory leakage problem because proper cleaning is not happening though we are always trying to clean the memory pointed by newNode, pRoot, pXMLDoc, pXMLBatchDoc pointers.

erwin_78
September 11th, 2003, 05:46 AM
i mean this line:
"if(pXMLDoc->load(fileName,&ret) == S_FALSE)"
this is in a loop and if a file is already loaded, i'm not sure if you can load it again and again with a new file, without clearing it.

sanjay soni
October 7th, 2003, 01:37 AM
erwin, It doesn't load single file again and again. It loads all the files, whose names and path has been stored in filelist vector.

giri75
February 25th, 2005, 04:10 AM
Hi sanjay,


Im facing certain issues with memory leak in MSXML, in the functions like cocreateinstance, load and other xml functions get_attributes() and so on..., this is affecting the performance of the software. could you please let me know how you resolved the issues earlier, it would be great help...

Thanks,
Girish

Mick
February 25th, 2005, 04:16 AM
You should probably create a new thread and post some of your code that you are using. This one is a year and a couple of months old :eek: