Questions with VARIANT and BSTR? 
Author Message
 Questions with VARIANT and BSTR?

Below is my source code:

        VARIANT a;

        VariantInit(&a);
        a.vt = VT_BSTR;
        a.bstrVal = ::SysAllocString(L"Hello");
        // operations on a
        VariantClear(&a); // -----<1>

        long lTemp = 100;
        VariantInit(&a);  
        a.vt = VT_I4;
        a.lVal = lTemp;
        // Operations on a
        VariantClear(&a);

My questiona are:
1) Does VariantClear(&a) at <1> release the memory
which is allocated for the bstrVal? If not, how could I
release the memory allocated to the bstr in the variant?

2) There are many VariantInit and VariantClear.
Could I rewrite the code above as follows? Is there any
memory leak?

        VARIANT a;

        VariantInit(&a);
        a.vt = VT_BSTR;
        a.bstrVal = ::SysAllocString(L"Hello");
        // operations on a

        long lTemp = 100;
        a.vt = VT_I4;
        a.lVal = lTemp;
        // Operations on a
        VariantClear(&a);

Thanks in advance!

George



Sun, 26 Sep 2004 00:04:53 GMT  
 Questions with VARIANT and BSTR?
1) Yes it does.
2) No. This code never frees the BSTR. By the time VariantClear is
called, the variant does not hold a BSTR anymore, so VariantClear does
not know it is supposed to free it.

You can remove a call to VariantInit after VariantClear. VariantClear
leaves the variant in an initialized state, holding VT_EMPTY. That's
about all you can optimize away here. Well, VariantClear on a variant
holding VT_I4 is, strictly speaking, unnecessary since no memory
deallocation is required, but it is a good idea to call it nevertheless.

Consider using smart wrappers such as CComVariant. They almost always
ensure that VariantInit and VariantClear are called as appropriate.
--
With best wishes,
    Igor Tandetnik

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


Quote:
> Below is my source code:

> VARIANT a;

> VariantInit(&a);
> a.vt = VT_BSTR;
> a.bstrVal = ::SysAllocString(L"Hello");
> // operations on a
> VariantClear(&a); // -----<1>

> long lTemp = 100;
> VariantInit(&a);
> a.vt = VT_I4;
> a.lVal = lTemp;
> // Operations on a
> VariantClear(&a);

> My questiona are:
> 1) Does VariantClear(&a) at <1> release the memory
> which is allocated for the bstrVal? If not, how could I
> release the memory allocated to the bstr in the variant?

> 2) There are many VariantInit and VariantClear.
> Could I rewrite the code above as follows? Is there any
> memory leak?

> VARIANT a;

> VariantInit(&a);
> a.vt = VT_BSTR;
> a.bstrVal = ::SysAllocString(L"Hello");
> // operations on a

> long lTemp = 100;
> a.vt = VT_I4;
> a.lVal = lTemp;
> // Operations on a
> VariantClear(&a);

> Thanks in advance!

> George



Sun, 26 Sep 2004 00:38:39 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. simple question about BSTR and VARIANT

2. VARIANT/BSTR IN VC Newbie question

3. VARIANT/BSTR IN VC Newbie question

4. memory leak with BSTR or VARIANT or other web browser MSHTML interfacing

5. Passing BSTR through VARIANT

6. memory leak with BSTR or VARIANT or other web browser MSHTML interfacing

7. about returning BSTR and VARIANT to the client

8. How to use VARIANT and BSTR in ATL

9. VARIANT to BSTR/LONG

10. How to implement Set and Get methods with VARIANT (bstr)

11. conversion of variant to bstr

12. Initializing VARIANT member of struct to contain a BSTR with initializer list

 

 
Powered by phpBB® Forum Software