Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VariantClear() ?

223 views
Skip to first unread message

vinod kumar

unread,
Mar 22, 2004, 8:23:23 AM3/22/04
to
Hi all,

I'm confused over using VariantClear() and other memory resources in the
following Get(...) method...
Is the following code correct ? or do any changes required ?

CComVariant pname;
CString processname;

hr = spInstance1->Get(CComBSTR(_T("Name")), 0,&pname, 0, 0);

if(SUCCEEDED(hr) && (V_VT(&pname) == VT_BSTR))
{
processname = (char *) OLE2T(V_BSTR(&procname));

VariantClear(&pname);
}
else
{
VariantClear(&pname);
}


The following link is the reference of this Get(....) method in msdn..

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
iwbemclassobject_get.asp

thank you,
vinod.


Alexander Nickolov

unread,
Mar 22, 2004, 4:23:15 PM3/22/04
to
About the only problem is the following line:

> processname = (char *) OLE2T(V_BSTR(&procname));

It should either use OLE2A, not cast to char*, or do nothing at all,
since CString can automatically convert from the wrong character
type (CString itself is TCHAR-mapped). Change it to:

processname = V_BSTR(&procname);

As for your question, I don't see any resource leakage off-hand,
do you have memory leaks? Calling VariantClear instead of the
class' Clear method is really bad style, but nothing harmful comes
out of it...

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

"vinod kumar" <naga_...@yahoo.com> wrote in message news:eOfFQDBE...@TK2MSFTNGP09.phx.gbl...

vinod kumar

unread,
Mar 23, 2004, 1:19:52 AM3/23/04
to
thanks Alex,

I have learn't that while using CComVariant we never have to release
anything. Are there any exceptions for this ?

Can you please refer me a web-link to learn in & out about
CComVariant,CComBSTR and CComPtr ?


thank you,
Vinod


"Alexander Nickolov" <agnic...@mvps.org> wrote in message
news:epXJgOFE...@TK2MSFTNGP11.phx.gbl...

Igor Tandetnik

unread,
Mar 23, 2004, 10:58:52 AM3/23/04
to
"vinod kumar" <naga_...@yahoo.com> wrote in message
news:O2fBQ7JE...@TK2MSFTNGP09.phx.gbl

> I have learn't that while using CComVariant we never have to release
> anything. Are there any exceptions for this ?

Yes. Something like this:

CComVariant val;
GetSomething(&val);
// val.Clear(); (1)
GetAnother(&val);

Without line (1), GetAnother will overwrite the contents of val, not
giving CComVariant any chance to clean up the old value first. This may
cause leaks.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


vinod kumar

unread,
Mar 24, 2004, 1:25:48 AM3/24/04
to
thanks,

what if GetSomething(&val) fails and val = NULL
Do we still have to do val.Clear() ?


thx,
vinod.


"Igor Tandetnik" <itand...@mvps.org> wrote in message
news:un#H##OEEHA...@TK2MSFTNGP11.phx.gbl...

Igor Tandetnik

unread,
Mar 24, 2004, 11:19:51 AM3/24/04
to
"vinod kumar" <naga_...@yahoo.com> wrote in message
news:ebWENjWE...@TK2MSFTNGP12.phx.gbl

> thanks,
>
> what if GetSomething(&val) fails and val = NULL
> Do we still have to do val.Clear() ?

You mean, val has the type of VT_NULL or VT_EMPTY? Not being a pointer,
it cannot really be equal to NULL.

Well, you only need to clear variants that hold allocated resources,
like BSTRs, safearrays or interface pointers. However, VariantClear()
(or its wrapper Clear()) correctly handles all variant types: if a
particular type does not require cleanup, VariantClear does nothing. So,
if in doubt - just call Clear().

0 new messages