COleVariant

来源:互联网 发布:live photos软件 编辑:程序博客网 时间:2024/04/29 17:06

COleVariant类是对VARIANT结构的封装。它的构造函数具有极为强大的功能,当对象构造时首先调用VariantInit进行 初始化,然后根据参数中的标准类型调用相应的构造函数,并使用VariantCopy进行转换赋值操作,当VARIANT对象不在有效范围时,它的析构函 数就会被自动调用,由于析构函数调用了VariantClear,因而相应的内存就会被自动清除。除此之外,COleVariant的赋值操作符在与 VARIANT类型转换中为我们提供极大的方便。

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


原创粉丝点击