0

Let's say I have the following scenario:

ComObjectClass firstCOMObject = new ComObjectClass();
ComObjectClass secondCOMObject = firstCOMObject;

Of course I have to release the firstCOMObject like this:

Marshal.FinalReleaseCOMObject(firstCOMObject);

But do I need to release secondCOMObject?
Can you verify your answers with MSDN or another article link?

3
  • 2
    This will only make your code crash. There is still only one object, your code just has two references to it. Review your favorite C# language book about reference types. Commented Jan 19, 2012 at 14:24
  • What about COM Callable Wrapper? msdn.microsoft.com/en-us/library/f07c8z1c.aspx Commented Jan 19, 2012 at 14:36
  • You should not use FinalReleaseCOMObject, but rather clear the references you are holding. Commented Jan 19, 2012 at 15:43

1 Answer 1

2

From:

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.finalreleasecomobject.aspx

The FinalReleaseComObject method releases the managed reference to a COM object. Calling this method is equivalent to calling the ReleaseComObject method in a loop until it returns 0 (zero).

So, no.

Sign up to request clarification or add additional context in comments.

2 Comments

What about COM Callable Wrapper? msdn.microsoft.com/en-us/library/f07c8z1c.aspx
@Dor, that's the other way (calling .NET code wrapped with a COM interface), and is handled with normal COM reference counting: "Unlike the .NET client it wraps, the CCW is reference-counted in traditional COM fashion. When the reference count on the CCW reaches zero, the wrapper releases its reference on the managed object. A managed object with no remaining references is collected during the next garbage-collection cycle." They are opposite directions of wrapping to your original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.