i'm just doing some wrap class for msxml and got some problem?

i'm using the raw interface in msxml.h, not smart pointer. (visual studio
2003)

sample code like this:
//--start-------------------------------------------
{
IXMLDOMDocument* pDoc = NULL;
HRESULT hr = ::CoCreateInstance(CLSID_DOMDocument, NULL,
CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**) &pDoc);

hr = pDoc->put_async(VARIANT_FALSE);
hr = pDoc->put_validateOnParse(VARIANT_FALSE);
hr = pDoc->put_resolveExternals(VARIANT_FALSE);

CComVariant varFile(_T("c:\\aa.xml"));
VARIANT_BOOL status;
hr = pDoc->load(varFile, &status);

BSTR bstrXML = NULL;
pDoc->get_xml(&bstrXML);

//***********************
// doing some processing for bstrXML;
// ...
//
SysFreeString(bstrXML); //should i do SysFreeString here?
//***********************

pDoc->Release();
}
//--end---------------------------------------------


i have used _CrtDumpMemoryLeaks() and
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF )
to detect memory leak.
but whether i do SysFreeString or not, there is no memory leak detected.

i believe use SysFreeString() here is a good behave.
and i have viewed some code from others. someone use, someone not.

i am very confused that is it necessary to do SysFreeString here?
and anyone can help me?

thanks!

Re: should i do SysFreeString here? by Jeff

Jeff
Wed Jun 01 23:35:17 CDT 2005

"aotin" <aotin@discussions.microsoft.com> wrote in message
news:3855E3CA-B224-48EB-8419-8584DA6165E9@microsoft.com...
>
> BSTR bstrXML = NULL;
> pDoc->get_xml(&bstrXML);
>
> //***********************
> // doing some processing for bstrXML;
> // ...
> //
> SysFreeString(bstrXML); //should i do SysFreeString here?

Yes.
--
Jeff Partch [VC++ MVP]



why? by aotin

aotin
Thu Jun 02 00:40:02 CDT 2005



"Jeff Partch [MVP]" wrote:

> "aotin" <aotin@discussions.microsoft.com> wrote in message
> news:3855E3CA-B224-48EB-8419-8584DA6165E9@microsoft.com...
> >
> > BSTR bstrXML = NULL;
> > pDoc->get_xml(&bstrXML);
> >
> > //***********************
> > // doing some processing for bstrXML;
> > // ...
> > //
> > SysFreeString(bstrXML); //should i do SysFreeString here?
>
> Yes.
> --
> Jeff Partch [VC++ MVP]
>
>
>

but why?
even i don't do that, there is no memory leak be detected?
can you show me more details about that.

thanks.

Re: why? by Jochen

Jochen
Thu Jun 02 00:53:58 CDT 2005

Hi aotin!

>>>BSTR bstrXML = NULL;
>>>pDoc->get_xml(&bstrXML);
>>>
>>>//***********************
>>>// doing some processing for bstrXML;
>>>// ...
>>>//
>>>SysFreeString(bstrXML); //should i do SysFreeString here?
>>
>>Yes.

>
> but why?
> even i don't do that, there is no memory leak be detected?
> can you show me more details about that.

Who should detect the leak?
This is not an CRT-Allocation, so CRT does not detect it...
And the allocation occurs inside the COM-Stub, so I do not know if tools
like Purify or other will detect the allocation...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Re: why? by aotin

aotin
Thu Jun 02 01:44:03 CDT 2005

>
> Who should detect the leak?
> This is not an CRT-Allocation, so CRT does not detect it...
> And the allocation occurs inside the COM-Stub, so I do not know if tools
> like Purify or other will detect the allocation...
>
> --
> Greetings
> Jochen
>


Oh. i see.

_CrtDumpMemoryLeaks() cannot detect the memory leak in com object.

i have tried another case:

//---------------------------------------
_CrtDumpMemoryLeaks();
{
BSTR bstrTMP = SysAllocString(L"ABCDEFG");
//...
//without: SysFreeString(bstrTMP);
}
_CrtDumpMemoryLeaks();
//---------------------------------------

_CrtDumpMemoryLeaks() also cannot detect the memory leak here.

so, in my first Q, the answer is:

The SysFreeString(bstrXML) is necessary to call by user to free the buffer.

thanks all of you guys.


Re: why? by Rodrigo

Rodrigo
Thu Jun 02 04:56:20 CDT 2005

This document could be interesting for you

http://www.winwonk.com/writing/commemory/


--
Un saludo
Rodrigo Corral González [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org



Re: why? by Simon

Simon
Thu Jun 02 05:37:13 CDT 2005

One method for checking for COM memory leaks (including BSTRs) is the
IMallocSpy method. Have a look at

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcedcom/html/cerefimallocspy.asp
and
http://www.microsoft.com/msj/0699/visualprog/visualprog0699.aspx

cheers

Simon

"aotin" <aotin@discussions.microsoft.com> wrote in message
news:F6B2A02A-FCF5-4632-97C6-0BF007C4B4E9@microsoft.com...
> >
> > Who should detect the leak?
> > This is not an CRT-Allocation, so CRT does not detect it...
> > And the allocation occurs inside the COM-Stub, so I do not know if tools
> > like Purify or other will detect the allocation...
> >
> > --
> > Greetings
> > Jochen
> >
>
>
> Oh. i see.
>
> _CrtDumpMemoryLeaks() cannot detect the memory leak in com object.
>
> i have tried another case:
>
> //---------------------------------------
> _CrtDumpMemoryLeaks();
> {
> BSTR bstrTMP = SysAllocString(L"ABCDEFG");
> //...
> //without: SysFreeString(bstrTMP);
> }
> _CrtDumpMemoryLeaks();
> //---------------------------------------
>
> _CrtDumpMemoryLeaks() also cannot detect the memory leak here.
>
> so, in my first Q, the answer is:
>
> The SysFreeString(bstrXML) is necessary to call by user to free the
buffer.
>
> thanks all of you guys.
>



Re: why? by Jochen

Jochen
Thu Jun 02 06:09:31 CDT 2005

> One method for checking for COM memory leaks (including BSTRs) is the
> IMallocSpy method. Have a look at
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcedcom/html/cerefimallocspy.asp
> and
> http://www.microsoft.com/msj/0699/visualprog/visualprog0699.aspx

You can also use my leak-finder:

http://blog.kalmbachnet.de/?postid=13


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Re: why? by aotin

aotin
Thu Jun 02 22:09:24 CDT 2005

"Rodrigo Corral [MVP]" wrote:

> This document could be interesting for you
>
> http://www.winwonk.com/writing/commemory/
>
>
> --
> Un saludo
> Rodrigo Corral González [MVP]
>
> FAQ de microsoft.public.es.vc++
> http://rcorral.mvps.org
>
>


it is a good article!

thanks a lot!!!

Re: why? by aotin

aotin
Thu Jun 02 22:36:01 CDT 2005

"Jochen Kalmbach [MVP]" wrote:

> > One method for checking for COM memory leaks (including BSTRs) is the
> > IMallocSpy method. Have a look at
> >
> > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcedcom/html/cerefimallocspy.asp
> > and
> > http://www.microsoft.com/msj/0699/visualprog/visualprog0699.aspx
>
> You can also use my leak-finder:
>
> http://blog.kalmbachnet.de/?postid=13
>
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
>

all of you are great. thanks a lot!