Solved

smart pointers leaking memory

Posted on 2006-06-14
14
1,466 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:chhakuli
  • 4
  • 3
  • 3
  • +1
14 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 43 total points
ID: 16908289
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
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 41 total points
ID: 16908961
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
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 41 total points
ID: 16909411
> 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
 

Author Comment

by:chhakuli
ID: 16912961
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
 
LVL 30

Expert Comment

by:Axter
ID: 16913081
>> 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:rstaveley
ID: 16913187
> 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
 

Author Comment

by:chhakuli
ID: 16916438
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 16918873
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
 

Author Comment

by:chhakuli
ID: 16938960
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
 
LVL 30

Expert Comment

by:Axter
ID: 16938985
>> have watched memory grow continuously in task manager

What memory is increasing?
Is Physical or Kernel Memory?
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16940146
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create string splitting user defined function in C++ 23 291
Building cUrl in Windows v7.43.0 4 28
Quicksort a dynamic deque 33 66
Arduino EDI - Programming language 3 80
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now