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);删除了。问题解决。

posted @ 2009-04-08 12:28  dzqabc  阅读(4109)  评论(0编辑  收藏  举报