Link to home
Start Free TrialLog in
Avatar of chhakuli
chhakuli

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chhakuli
chhakuli

ASKER

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?
>> 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.
> 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.
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.
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<--------
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'.
>> have watched memory grow continuously in task manager

What memory is increasing?
Is Physical or Kernel Memory?
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.]