Re: Memory leak

From: Siv (anonymous_at_discussions.microsoft.com)
Date: 02/26/04


Date: Thu, 26 Feb 2004 05:39:13 -0800


Thanks for the reply.

I have been using the function for long without any
problem, may be thats because all my XML messages were
short(less than 500char ). Any how i will try and add the
suggetions.

Why i say there could be a memory leak is
1. in sytem-settings-memeory the inuse memeory for
programs increases rapidly when this app' is run
2. after 15 min or so the app' freezes as there is not
enough program memory in the IPAQ.

Since it works for 15min or so and then freezes, made me
to think it was a memeory leak than a memory corruption.

I was wondering could XML COM compoenet is not properly
relased?

Siv
 

>-----Original Message-----
>I haven't looked thoroughly through your code snippet,
but the biggest thing
>that worries me is the potential for memory overwrites
(memory corruption).
>
>For example, in these line:
>
>> strncpy(m_XMLMessage,(const char*) XML, strlen(XML)+1);
>
>Is it possible XML string is larger than m_XMLMessage
buffer will
>accomodate? You probably want 1024, or better, sizeof
(m_XMLMessage),
>instead of strlen(XML)+1.
>
>> m_XMLMessage[strlen(m_XMLMessage) + 1] = '\0';
>
>The available subscripts on a string, just like any
array, are 0..n, where
>n=size of the array (in chars, or elements) - 1.
>In your case, you can go from 0-1023. And the max NULL-
terminated string
>you could have is 1023 characters plus a NULL terminator -
 1024 characters
>total.
>
>If, in your case, the string was exactly the number of
characters you could
>have (1023), then strlen(m_XMLMessage) is 1023, that + 1
is 1024, and you
>have corruption. You don't want the "+1".
>
>You also have calls to mbstowcs and wcstombs that I
see "+1"'s in. Again I
>didn't go through this thoroughly, but I'm betting that's
your problem.
>
>Btw, you probably don't want a number like "60" in your
code. You should
>probably have a sizeof() or #define or just about
anything other than 60. I
>see you declared your buffer that long, but I'm most
worried about the call
>to wcstombs that uses it. As soon as you change that 60
to something
>smaller, you're toast. You sizeof(m_CommandType).
>
>Why do you assume a leak?
>
>Also, I would review all my code for similar problems.
>--
>
>Michael Salamone [eMVP]
>Entrek Software, Inc.
>www.entrek.com
>
>
>"Siv" <anonymous@discussions.microsoft.com> wrote in
message
>news:22b901c3fc56$3d4d77e0$a401280a@phx.gbl...
>> Hi,
>>
>> There is a a memory leak in a application, and i think
>> the leak is caused by the following function, which is
>> called in the main application many times( in while a
>> loop). Moreover after running the application for 15min
>> the following function fails. Below is the function. Can
>> anyone spot a memory leak in the function???
>>
>> ////////////////////////////////////////////////////////
///
>> ////////////////////////////////
>> //Returns tag value for a given tag name from XML event
>> notification
>> //IN tag name
>> //return tag value
>> char* GetTagValueFromXMLmessage(char* XML,char* tagName)
>> {
>>
>>
>> char m_XMLMessage[1024];
>> strncpy(m_XMLMessage,(const char*) XML, strlen(XML)+1);
>> m_XMLMessage[strlen(m_XMLMessage) + 1] = '\0';
>>
>> char m_tagName[100];
>> strncpy(m_tagName,(const char*)tagName,strlen(tagName)
+1);
>> m_tagName[strlen(m_tagName) + 1] ='\0';
>>
>> IXMLDOMDocument *iXMLDoc = NULL;
>> IXMLDOMNodeList *iXMLNodeList = NULL;
>> IXMLDOMNode *iXMLNodeCommandPtr = NULL;
>>
>> HRESULT hr;
>> VARIANT_BOOL tEmpty;
>>
>> BSTR bstrCommandText;
>> wchar_t pwc_xml[sizeof( wchar_t )*1024];
>> wchar_t pwc_tagName[sizeof( wchar_t )*100];
>>
>> char m_xml[1024];
>> char m_CommandType[60];
>>
>> _try
>> {
>>
>> //create instance of msXML COM componenet
>> hr = CoInitializeEx(NULL,COINIT_MULTITHREADED);
>> if(!SUCCEEDED(hr)) return 0;
>>
>> hr = CoCreateInstance (CLSID_DOMDocument, NULL,
>> CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
>> IID_IXMLDOMDocument,(LPVOID *)&iXMLDoc);
>>
>> if(iXMLDoc)
>> {
>> iXMLDoc->put_async(VARIANT_FALSE);
>>
>> //convert xmlMessage to wchar
>> int ii = mbstowcs( pwc_xml, m_XMLMessage, strlen
>> (m_XMLMessage) + 1 );
>>
>>
>> //Load XML
>> iXMLDoc->loadXML( pwc_xml, &tEmpty);
>>
>>
>> //convert tagName to wchar
>> ii = mbstowcs( pwc_tagName,m_tagName, strlen(m_tagName)
>> +1 );
>>
>> //get command node
>> //iXMLDoc->getElementsByTagName
(L"Value",&iXMLNodeList);
>> iXMLDoc->getElementsByTagName
(pwc_tagName,&iXMLNodeList);
>>
>> iXMLNodeList->get_item(0, &iXMLNodeCommandPtr);
>>
>> iXMLNodeCommandPtr->get_text(&bstrCommandText);
>> ii = wcstombs( m_CommandType, bstrCommandText, 60 );
>>
>> }//end if
>>
>> //Release memory
>> iXMLNodeList->Release();
>> iXMLNodeList = NULL;
>> iXMLNodeCommandPtr->Release();
>> iXMLNodeCommandPtr = NULL;
>> iXMLDoc->Release();
>> SysFreeString(bstrCommandText);
>>
>>
>>
>>
>> }
>> __except(EXCEPTION_EXECUTE_HANDLER)
>> { int di;
>> printf("ERROR in ..XML parse for tag: %
>> s\n",m_tagName);
>> scanf("%d",&di);
>>
>> }
>>
>> char a_m_xml[1024];
>> strncpy(a_m_xml,m_CommandType, strlen(m_CommandType)
>> +1);
>>
>> return a_m_xml;
>>
>>
>>
>> }
>>
>> Any replies is much appreciated
>>
>> Thanks
>> Siv
>
>
>.
>



Relevant Pages

  • Re: comparison between portability libraries?
    ... E.g. in the XML case, accepting the full spec of e.g. an XML ... and name examples purely from memory. ... new on-heap version or whatever (string duplication is done almost ... duplication is really rather frequent. ...
    (comp.lang.misc)
  • RE: GC memory pressure problem in using unmanaged components
    ... There is a probablity of memory leak but i dont think ... that the given exception is a result of memory leak. ... Int32 height, String& ext) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: How to convert a Dataset into a xml strin
    ... Services) to return/take dataset instead of string. ... It also very slow and takes tons of memory. ... DataSet with schema in any form (Typed DS with embedded schema, ... If XML string you sending was not produced by DataSet, ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: add Node into XML file using XMLTextWriter without using XMLDo
    ... I think it'd be better performance-wise to create the xml as a plain string, ... required for DOM objects ... ... >> Essentially it would have to load the entire document in memory just to ...
    (microsoft.public.dotnet.general)
  • Re: add Node into XML file using XMLTextWriter without using XMLDo
    ... Say your xml starts with "" and that's where you want to insert the new ... Work with strings, i.e. load the file as a string in memory and ... Use streams to make things a little faster. ... perspective, but, if implemented properly, it will cut-down much on memory ...
    (microsoft.public.dotnet.general)

Loading