memory leak

15 views
Skip to first unread message

no...@none.com

unread,
Dec 9, 2002, 9:20:02 AM12/9/02
to

I see a memory leak in VB 6 service pack 5 applications when I use the
following code to trigger a connection point event ( this is the code in
__ComInvokeEventHandler() called by __raise ):

EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof excepInfo);
UINT nArgErr = (UINT)-1;
HRESULT hr = pDispatch->Invoke(id, IID_NULL, LOCALE_USER_DEFAULT, wFlags,
pDispParams, pVarResult, &excepInfo, &nArgErr);
IErrorInfo* perrinfo = 0;
_com_handle_excepinfo(excepInfo, &perrinfo);
if (FAILED(hr)) {
SetErrorInfo(0, perrinfo);
}

But I do not see the memory leak if I pass in NULL for &excepInfo and
&nArgErr. Has anyone else seen this occur? or is there is a fix out for it?
I have the latest service packs, the COM+ rollup and I am using win2000.

Thanks

Alexander Nickolov

unread,
Dec 9, 2002, 3:50:08 PM12/9/02
to
You never freed the returned exception info structure - it contains
embedded BSTRs you are supposed to SysFreeString...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

<no...@none.com> wrote in message news:eie3M44nCHA.2444@TK2MSFTNGP10...

no...@none.com

unread,
Dec 9, 2002, 4:56:42 PM12/9/02
to
>
>"Alexander Nickolov" <agnic...@mvps.org> wrote in message
news:<uYAf7R8nCHA.2168@tkmsftngp07>...

>You never freed the returned exception info structure - it contains
>embedded BSTRs you are supposed to SysFreeString...
>

This is not my code, this is microsoft's code which is automatically
generated using the __raise compiler statement. You can see this stepping
through the __raise code in a disassembler.

If this struct does contain embedded BSTRS, then the free code will need to
be added into the __ComInvokeEventHandler() function which is in the ATL
source code.


Alexander Nickolov

unread,
Dec 10, 2002, 1:41:56 PM12/10/02
to
Sorry, I don't have VC7 at hand. Can you check the implementation
of _com_handle_excepinfo? Perhaps it has code to free the struct
fields... If it doesn't, this is a clear bug.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

<no...@none.com> wrote in message news:OdLIZ38nCHA.2396@TK2MSFTNGP08...

Alexander Nickolov

unread,
Dec 10, 2002, 1:43:52 PM12/10/02
to
Sorry, I reread your post. If you are passing in the excepinfo, you
are the one to free it up...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

<no...@none.com> wrote in message news:OdLIZ38nCHA.2396@TK2MSFTNGP08...

no...@none.com

unread,
Dec 10, 2002, 1:56:23 PM12/10/02
to
I'm not writing any code other than:

__raise MyFunc()

All other code is generated by the compiler.

Alexander Nickolov

unread,
Dec 10, 2002, 2:10:03 PM12/10/02
to
Well, then the bug will be in the generated code as per my previous
reply. Did you check the code for _com_handle_excepinfo?

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

<no...@none.com> wrote in message news:#ypJS3HoCHA.2312@TK2MSFTNGP10...

Alexander Nickolov

unread,
Dec 10, 2002, 3:32:26 PM12/10/02
to
Honestly, I don't see anything to leak here... And what does this mean
in your original post?

===================


But I do not see the memory leak if I pass in NULL for &excepInfo and
&nArgErr.

===================

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

<no...@none.com> wrote in message news:ectejGIoCHA.2312@TK2MSFTNGP12...
>
> I looked and expinfo, and all members of exepinfo are NULL so VB 6 is not
> returning any BSTR's to free.
>
> But it still leaks quite well. I attached a sample project, but I do not
> know if it will go to the newsgroup properly. I also sent the project to
> your email address.


>
> "Alexander Nickolov" <agnic...@mvps.org> wrote in message

> news:#ecjI9HoCHA.1536@tkmsftngp02...

no...@none.com

unread,
Dec 10, 2002, 4:00:46 PM12/10/02
to
>Honestly, I don't see anything to leak here...

By visual inspection, I do not see a place where a leak would occur and I am
guessing it is somewhere inside VB 6.

Whenever I press the "LeakMe" button in the sample project I sent you it
leaks approx. 4 MB of memory. The "LeakMe" button triggers 100000
connection point events each time you hit it. I have tested the sample code
on 20 different windows 2000 machines with different levels of service packs
and the program leaks in every case.

>But I do not see the memory leak if I pass in NULL for &excepInfo and
>&nArgErr.

It means the following code does not cause a memory leak:

HRESULT hr = pDispatch->Invoke(id, IID_NULL, LOCALE_USER_DEFAULT, wFlags,

pDispParams, pVarResult, NULL, NULL );

But the following code does cause a memory leak:

HRESULT hr = pDispatch->Invoke(id, IID_NULL, LOCALE_USER_DEFAULT, wFlags,
pDispParams, pVarResult, &excepInfo, &nArgErr);

"Alexander Nickolov" <agnic...@mvps.org> wrote in message

news:#FW#rsIoCHA.1896@tkmsftngp04...

no...@none.com

unread,
Dec 10, 2002, 4:06:33 PM12/10/02
to

If it helps at all, the code generated in VC6 is:

CComVariant varResult;
T* pT = static_cast<T*>(this);
int nConnectionIndex;
int nConnections = m_vec.GetSize();

for (nConnectionIndex = 0; nConnectionIndex < nConnections;
nConnectionIndex++)
{
pT->Lock();
CComPtr<IUnknown> sp = m_vec.GetAt(nConnectionIndex);
pT->Unlock();
IDispatch* pDispatch = reinterpret_cast<IDispatch*>(sp.p);
if (pDispatch != NULL)
{
VariantClear(&varResult);
DISPPARAMS disp = { NULL, NULL, 0, 0 };
pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&disp, &varResult, NULL, NULL);
}
}
return varResult.scode;

This code does not leak at all in VB 6 service pack 5.


Alexander Nickolov

unread,
Dec 10, 2002, 5:49:51 PM12/10/02
to
You can do a simple test - create an ATL client to sink your events
and check if the same leak occurs. If it disappears, and as you are
sure the excepinfo does not contain embedded BSTRs upon return,
your conclusion about VB leaking the memory is very plausible...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

<no...@none.com> wrote in message news:uWiJBAJoCHA.2332@TK2MSFTNGP08...

Alexander Nickolov

unread,
Dec 11, 2002, 2:06:20 PM12/11/02
to
I'll bring this thread to MS attention. However, unless you open
an incident with MS support, I don't think you'll get anything
other than a bug fix... :)

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

<no...@none.com> wrote in message news:uGn6EUToCHA.2540@TK2MSFTNGP12...
> This also leaks in C#, I included a sample C# project in the ZIP file.
>
> If you cant read the zip file from the newsgroup, let me know and I can sent
> it directly to you.
>
> As of yet, I have not tested with a ATL client, but my guess is the ATL
> client would not leak. In my opinion this is a serious enough problem in C#
> and VB that it warrents fixing (unless its already fixed and I missed it, or
> unless I'm doing something really stuipd!).
>
> Who can I send this to in microsoft, maybe I can get a free t-shit or
> something if it turns out to be a bug :)
>
>
>
>

no...@none.com

unread,
Dec 11, 2002, 5:27:59 PM12/11/02
to
Thanks for your help

"Alexander Nickolov" <agnic...@mvps.org> wrote in message

news:OlSotfUoCHA.2292@tkmsftngp02...

Santhosh Pillai [MS]

unread,
Dec 11, 2002, 8:46:58 PM12/11/02
to
If you open a support incident with developer support, we sure can get this
fixed if it turns out to be bug.
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

Thanks,
Santhosh
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.
©2002 Microsoft Corporation. All rights reserved.

Reply all
Reply to author
Forward
0 new messages