smart pointers leaking memory

It looks like smart pointers used to parse xml files like IXMLDOMDocument2Ptr, IXMLDOMElementPtr, IXMLDOMNodePtr etc leak memory. We are re-using these pointers in some functions reading xml files. Since we started using xml files in place of string tables, users started getting 'Low virtual memory' error. I also noticed in Task manager that the memory usage goes on increasing as we use the application. Does anyone has any idea about this and how to fix this? Is this a known issue? Any workaround? Please help.
chhakuliAsked:
Who is Participating?
 
AxterConnect With a Mentor Commented:
Hi chhakuli,
You can try using memory leak detection tools, or you can try a library like the following:
http://code.axter.com/leaktracker.h

Cheers!
0
 
Deepu AbrahamConnect With a Mentor R & D Engineering ManagerCommented:
Hi,

http://www.glowcode.com/summary.htm is a good tool for memory leak detection.It will tell you which function is leaking memory and has got a good GUI.

Best Regards,
DeepuAbrahamK
0
 
rstaveleyConnect With a Mentor Commented:
> Since we started using xml files in place of string tables

If you are using XML in the place of string tables, isn't it the case that your XML is loaded for the entire lifetime of your application? If so, you wouldn't be aware of leaks, because the XML would be permanently loaded and you wouldn't expect to get the memory back until the application exits.

It may be the case that you are not leaking, you simply require the high memory overhead of using DOM rather than SAX. With DOM the entire document is loaded in RAM in a node structure. However, I guess SAX isn't an option for you because you need those strings to be immediately available. In your case, your XML document (string requirement) requires more than the available physical RAM, which means that you are using virtual memory.

Appoximately how many bytes of string data are you loading? How does that compare against your physical memory and kernel memory in Windows Task Manager?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
chhakuliAuthor Commented:
I am 100% sure there is memory leak. The XML files are not very big, but we are reading them multiple times, whenever something is required from the file. I have watched memory grow continuously in task manager if I kept refreshing my application.
I would like to know how to take care of the leakage. Is there something else I can use in place of DOM? My application is in VC++ 6.0. Is there a way to fix leakage caused by DOM smart pointers?
0
 
AxterCommented:
>> have watched memory grow continuously in task manager

What memory is increasing?
Is Physical or Kernel Memory.

Be aware, that Windows OS has lazy logic, that will not release memory until it's needed.
This is especially true of Kernel Memory for both Paged and Nonpaged.
0
 
rstaveleyCommented:
> but we are reading them multiple times, whenever something is required from the file

In which case, can you use SAX rather than DOM?

However, let's not throw the baby out with the bath water...

> IXMLDOMDocument2Ptr, IXMLDOMElementPtr, IXMLDOMNodePtr etc leak memory

Are you remembering call call Release() on all of these? They may be "smart pointers", but I notice in the MSXML documentation examples that they always call Release() on these before they go out of scope. It is certainly worth a try.
0
 
chhakuliAuthor Commented:
rstaveley,
I am not really sure we should call Relase() on smart pointers.
Suppose I am re-using IXMLDOMElementPtr pointer in one of my function. Does every re-use increase its reference count? Should I call Release multiple times then? Can you send link to some example code?

I am also using IXMLDOMNode. Is this a struct or smart pointer? Should I call Release on it too?

Please help. I am desperately looking to resolve this.
0
 
rstaveleyCommented:
Here's an example with explicitly calls Release: http://msdn.microsoft.com/library/en-us/xmlsdk/html/145c3703-228b-4bd4-a703-4066f23f1ab1.asp

I'm also finding examples that do not. Notably the example you find when you look for IXMLDOMElementPtr in the Microsoft XML Core Services (MSXML) 4.0 help file doesn't call Release.

Do you see the leak sufficiently quickly that you can try calling Release on all your IxxxPtr objects just before they go out of scope to see if it makes a difference?

Sorry, I'm not being much help.

One thought, though... are you compiling with VC 6 or 7?

The reason I ask is that VC 7.1 seems to have IXMLDOMDocumentPtr defined in "c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\comdefsp.h" as a _COM_SMARTPTR_TYPEDEF and VC 6 does not.

With VC 7.1 you need to prefix IXMLDOMDocumentPtr explicitly with the namespace MSXML2 to get the version generated from the msxml4.dll import. It is conceivable that the smart pointer implementations differ and one needs the Release and the other doesn't??

Here's the example from the MSXML 4.0 help file, with the explicit MSXML2 namespace prefix required for VC 7.1. Note the lack of Release() in this example.
--------8<--------
// Compile with /MT

#include "afx.h"
#import "msxml4.dll"
using namespace MSXML2;

inline void TESTHR( HRESULT _hr )
   { if FAILED(_hr) throw(_hr); }

void XMLDOMElementSample()
{
   try {
      // Explicit MSXML2 namespace prefix needed here for VC 7.1 to
      // Get the interface from the import.
      MSXML2::IXMLDOMDocumentPtr docPtr;
      MSXML2::IXMLDOMElementPtr ElementPtr;

      //init
      TESTHR(CoInitialize(NULL));
      TESTHR(docPtr.CreateInstance("Msxml2.DOMDocument.4.0"));

      // Load a document.
      _variant_t varXml("C:\\book.xml");
      _variant_t varOut((bool)TRUE);
      varOut = docPtr->load(varXml);
      if ((bool)varOut == FALSE)
         throw(0);
      ElementPtr = docPtr->documentElement;
      MessageBox(NULL, ElementPtr->xml, _T("Document from its root"), MB_OK);
   } catch(...)
   {
      MessageBox(NULL, _T("Exception occurred"), _T("Error"), MB_OK);
   }
   CoUninitialize();
}

int main()
{
   XMLDOMElementSample();
   return 0;
}
--------8<--------
0
 
chhakuliAuthor Commented:
I am using Visual Studio .NET 2003 for compiling the project. I tried to call Relase and setting it to NULL after it, but it does not seem to help. Memory usage still goes up as I continue using my application. Yes, all my pointer definitions are prefixed with MSXML2. But we are importing 'msxml3.dll' and not 'msxml4.dll'. Does it make a difference? We are not sure if all our user machines have 'msxml4.dll' on thier machine and so we use 'msxml3.dll'.
0
 
AxterCommented:
>> have watched memory grow continuously in task manager

What memory is increasing?
Is Physical or Kernel Memory?
0
 
rstaveleyCommented:
Can you try an msxml4 version on a machine, which has it installed? It is a long shot, because I notice that msxml3 had a garbage collection issue resolved in Windows CE 5.0 - see http://support.microsoft.com/kb/914083/en-us, but nothing else is showing up in the Knowledge Base at http://support.microsoft.com/search/default.aspx?catalog=LCID%3D1033&spid=global&query=msxml3+leak&adv=&mode=r&cat=False

[If you do need to ship software which requires msxml4, the installer isn't too painful.]
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.