Sunday 15 February 2015

.net - Why use FinalReleaseComObject instead of ReleaseComObject? -


I know that the original difference in the form of ReleaseComObject only decreases some counter and FinalReleaseComObject decreases it to zero

So what I've usually heard, call FinalReleaseComObject because then you are certain that the COM object actually released

But this makes me wonder, is there a point right over this counter? Do not you break that system, if you always call the last com object if you call that counter on a ReleaseComObject Not the first, so there is no reason for this?

This may be the reason for having more than one when it should not be?

Thanks in advance.

PS: My COM experience only consists of using Excel Interop. Not sure that this question is local for the domain (i.e. the external office interop, finalReleaseComObject is not often used).

Update 1

Donation Talk When you are using ReleaseComObject as I think, this is a normal way. I think that if you do this then it should work fine. In a comment for the author, it is reported to anyone that it releases "code"> ReleaseComObject until the release actually (since 2006, so it is calling FinalReleaseComObject Is similar to). But he also says that it can be dangerous.

If you really want RCW to make a release () call at a particular point in the code, you can call the Release Com Object () in Rob until the global value reaches zero. Are there. It should be ensured that RCW will call the release () However, if you do so, it is warned, when other managed references try to use that RCW, it will create an exception. "

I believe this is a good idea to actually call not always the end of the com subject Because you can cause exceptions elsewhere, as I have seen it now, you should call it only if you can make sure that you can. FinalReleaseComObject is a hack of a best practice. >

Some prelude ...

A runtime callable cover (RCW) only IUnknown. Call AddRef, which is once on an unmanaged COM interface that wrap it up. However, an RCW is also managed by a managed number. Maintains a separate count of the number of Rbhon it here Arseedyu. It has been reduced by calling Marshall a different country managed contexts. Release Com Object When count of managed references reaches zero, then call RCW IUnknown.Release at once unmanaged COM interface.

Marshall.FinelRelicom object takes a managed reference number for zero with a call, and immediately unmanaged IUnknown.Release method wrapped in this way (assuming that the managed reference count was not already zero ).

Why both are martial? Release Comm Object and Marshall FuneralRelease Com Object? Calling Marshall.FinalRailCom Object Bus avoids writing a loop that calls to Marshall. Unless it does not indicate unless you want to indicate that you have finished using actually the COM object now .

Why use Marshall either. Release Com Object or Marshall: FinalRailCom Object? There are two reasons for which I know:

The first thing to do is to ensure that unmanaged resources (such as file handles, memory etc.) are used by wrapped COM objects as soon as possible unmanaged Results of calls as a result of the IUnknown.Release () method.

The second is to make sure that thread has unmanaged IUnknown.Release () method under your control, and there is no final form of thread.

In these martial methods, the end of RCW will finally call the unmanaged IUnknown.Release () method, after some time the RCW trash was collected.

For reliable details, see Visual C ++ Team Blog Entry


No comments:

Post a Comment