VariantClear 和 VariantInit

 

  VariantClear  和 VariantInit 函数两个函数是做什么的呢?近日程序出错,我不得不认真研究了一下它们的作用。

 

1.VariantClear ()函数

Clears the contents of a variant and sets the variant to VT_EMPTY.

如何做到Clears the contents of a variant (清空变量的内容呢)? 实际是通过调用相应变量类型的释放空间的方法来达到这个目的的。而且他不仅释放了空间,还初期化了变量。简单的英文说明,不译了。

Comments

VariantClear is used to clear the contents of initialized variants. Use VariantClear before a variant variable goes out of scope (i.e. to release the contents of local variants before leaving a subroutine or function).

 

VariantClear sets the content of VarDest to VT_EMPTY. In addition, if VarDest had a reference to a BString or Safe Array, VariantClear calls StrFree to release the memory use by the BString or SafeArrayDestroy to destroy the contained Safe Array. If VarDest had a reference to an object, VariantClear calls the object’s Release method.

 

You don’t need to call VariantClear for variants that contain values that are not dynamically allocated. For example, you don’t need to call this subroutine to clear a variant containing a VT_I4 (an integer) or a VT_R4 (real) value. Note, however, that failure to call VariantClear on variants that contain dynamically allocated values may result in memory leaks, because the BString or Safe Array referenced by the variant may never be released.

 

2.VariantInit函数

Initializes a variant to VT_EMPTY

他的作用很简单,就是初期化为VT_EMPTY。其实这个VariantClear ()已经做了,如果如果调用了它,就可以不用VariantInit()了。

 

3.说说我遇到的问题

一直出现内存访问冲突的错误,我用注释的方法,最后终于定位到了一行代码。

SafeArrayDestroy(sa);

VariantClear (&sa);

只要调用VariantClear (&sa);就出错,后来只好删除了,没事了。查了函数说明才明白,原来是因为sa的空间释放了两次啊。而且VariantClear (&sa);这个可以根据类型释放空间,感觉更好些,最后就把SafeArrayDestroy(sa);删除了。问题解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值