zoukankan      html  css  js  c++  java
  • 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);删除了。问题解决。

  • 相关阅读:
    如何通過編程獲取列表項目的附件以及多行文本中的文件內容
    小技巧:如何管理保存在本地的用户凭据
    WF 4.0中如何实现xaml工作流的动态加载
    使用jquery构造自己的多级菜单
    和安蕾尔的合影
    360太tmd脑残了
    3D 打印机技术设想
    再放2张数字油画
    解决问题的艺术:半小时编程实现照片的反转负冲特效
    承接数字油画图稿/线条图定制(出图)业务
  • 原文地址:https://www.cnblogs.com/dongzhiquan/p/1994916.html
Copyright © 2011-2022 走看看