[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

wpcmame

10/7/2007 8:14:00 PM

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())

2. How do I release a COM object.

- With one instance it is easy.

ComClass obj = new ComClass();
....
ReleaseComObject(obj);

- 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?

- What if one instance is "temporary" and doesn't have a name

ComClass2 obj = (ComClass2)(new ComClass1());
How many RCW's do I have now?
3 Answers

(Mattias Sjögren)

10/7/2007 9:17:00 PM

0

>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?


>2. How do I release a COM object.
>
>- With one instance it is easy.
>
>ComClass obj = new ComClass();
>...
>ReleaseComObject(obj);
>
>- 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).


>- What if one instance is "temporary" and doesn't have a name
>
>ComClass2 obj = (ComClass2)(new ComClass1());
>How many RCW's do I have now?

That doesn't change anything, there's still only one RCW.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.n... | http://www.dotneti...
Please reply only to the newsgroup.

wpcmame

10/8/2007 7:35:00 AM

0

> >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.


Rexxowski

10/8/2007 8:17:00 AM

0

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().