Rexxowski
10/8/2007 8:17:00 AM
wpcmame napsal(a):
>>> 1. If Marshal.GetNativeVariantForObject allocates memory on the (e.g. for a
>>> string or an array) how do I free this memory? (i.e. the managed version of
>>> VariantClear())
>> I don't think there is one, what's wrong with calling VariantClear?
>
> Nothing wrong. Just odd that there is a StringToBSTR/FreeBSTR pair in the
> Marshal class but only one way for variants.
>
>>> - but what if I have two instances
>>>
>>> ComClass1 obj1 = new ComClass1();
>>> ComClass2 obj2 = (ComClass2)obj1;
>>>
>>> Do I now have one RCW with a refcount of 2 or 2 RCW:s with a refcount of 1?
>>> Should I call ReleaseComObject on both obj1 and obj2?
>> You have one RCW and it holds one reference to the underlying COM
>> object. If you call ReleaseComObject on obj1 it will also invalidate
>> obj2 (since they refer to the same object).
>
> In the ReleaseComObject documentation it says:
> "The runtime callable wrapper has a reference count that is incremented
> every time a COM interface pointer is mapped to it. "
>
> My understanding of that sentence is that the cast to another interface will
> increase the refcount on the RCW so I need to call ReleaseComObject twice to
> release it.
>
>
Yes, your understanding is quite correct. That's why there is
Marshal.FinalReleaseComObject() method. You can either call
ReleaseComObject() in a loop until it returns zero or call
FinalReleaseComObject().