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.
> 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...
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...
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
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...
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().