Solved

smart pointers leaking memory

Posted on 2006-06-14
14
1,461 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

20 Experts available now in Live!

Get 1:1 Help Now