none
GetObjectsForNativeVariants and VariantClear RRS feed

  • Question

  • I am calling a method that returns an array of variants (http://msdn.microsoft.com/en-us/library/dd311957(VS.85).aspx ).  The docs say "The caller must free each VARIANT by calling VariantClear. The caller must also free the array by calling CoTaskMemFree. "

    GetObjectsForNativeVariants does a great job of reading the variants.  However, the docs are completely silent about whether GetObjectsForNativeVariants clears the variants as it loads them into the array.  Does GetObjectsForNativeVariants use VariantCopy to copy the elements?  Or will the eventual destruction of the returned array do all the releasing that is necessary?

    If GetObjectsForNativeVariants *doesn't* release the variants, do I need to create my own interop def for VariantClear as well?  I don't see anything under Marshal.  Did I miss it?

    Thanks.
    Monday, June 22, 2009 6:18 AM
    Moderator

Answers

  • Hard to say, Rotor doesn't include it.  Pretty unlikely.  But very easy to find out for yourself: just call it twice. 

    VariantClear() is declared in the framework but not accessible.  Use Reflector to copy the code from WindowBase.dll, you'll want MS.Win32.UnsafeNativeMethods.ArrayToVARIANTHelper.FreeVariantVector().

    Hans Passant.
    Monday, June 22, 2009 10:57 AM
    Moderator

All replies

  • Hard to say, Rotor doesn't include it.  Pretty unlikely.  But very easy to find out for yourself: just call it twice. 

    VariantClear() is declared in the framework but not accessible.  Use Reflector to copy the code from WindowBase.dll, you'll want MS.Win32.UnsafeNativeMethods.ArrayToVARIANTHelper.FreeVariantVector().

    Hans Passant.
    Monday, June 22, 2009 10:57 AM
    Moderator
  • Thanks for the response.

    I've experimented further.  Since GetObjectsForNativeVariants takes a pointer, I can open a memory window and watch what happens to the bytes when I call GetObjectsForNativeVariants .

    Calling GetObjectsForNativeVariants does not cause any change to the bytes.
    Calling VariantClear from oleaut32 does cause the bytes to be overwritten.

    Given how Variants work, .Net could (in theory) be doing some sort of "take ownership" thing, and will release the memory when the .net object is destroyed.  But that seems way unlikely.  More likely is that your first reaction was right, and it makes a copy of the data into managed memory.  So the Variants must be cleared manually using a method that .Net doesn't provide. 

    Not to be too critical here, but the designer of this method could perhaps have taken a bit more time considering how it would be used.

    In any case, if you have friends in the documentation group at MS, you might suggest they add a line or two to the docs here.  Both to make it clear that the method doesn't free the variants, and perhaps some thoughts on what you will need to do to actually free them.

    Thanks.
    Tuesday, June 23, 2009 12:17 AM
    Moderator
  • Use connect.microsoft.com to ask for doc updates.

    Hans Passant.
    Tuesday, June 23, 2009 12:23 AM
    Moderator