Can I get some clarity in ReleaseComObject? Can I ignore this?

I have been developing office solutions in VBA for some time now and have quite comprehensive knowledge in the field of office development in VBA. I decided it was time to learn some real programs with .Net, and I have problems with slots.

After looking at a bunch of articles and forums (here and elsewhere), there seems to be some mixed information about memory management in .NET when using COM objects.

Some people say that I should always determine the release of COM objects, while others say that I will almost never do this.

People say I have to do this:

Excel Professional Development Book on page 861.

This question about file sharing answered: "Every link that you make to a COM object must be released. If you do not, the process will remain in memory"

This blog suggests using it to solve your problems.

People say that I should not do this:

Eric Carter's MSDN blog post says, "In VSTO scripts, you usually don't need to use ReleaseCOMObject."

The book "VSTO for Office 2007" , which co-authored with Eric Carter, does not seem to mention memory management or ReleaseComObject.

This Paul Harrington MSDN blog says don't do this.

Someone with mixed advice:

Jake Ginnivan says that I should always do this on COM objects that do not leave the scope of the method. If a COM object leaves the method scope, forget about it. Why can't I just forget about it all the time?

Paul Harrington's blog seems to suggest that MS's advice has changed once in the past. Is this the case when calling ReleaseCOMObject has become best practice, but no longer exists? Can I leave the finer details of MS memory management and assume that everything will be mostly beautiful?

+6
source share
2 answers

I try to adhere to the following rule in my interaction development regarding ReleaseComObject .

If my managed object implements some kind of shutdown protocol similar to IDisposable , I call ReleaseComObject on any child COM objects on which the links are stored. Some examples of shutdown protocols I'm talking about:

  • IObjectWithSite.SetSite(null)
  • IOleObject.SetClientSite(null)
  • IOleObject.Close()
  • IDTExtensibility2.OnDisconnection
  • IDTExtensibility2.OnBeginShutdown

  • IDisposable.Dispose

This helps break down potential circular links between .NET and native COM objects, so a managed garbage collector can do its job without spaces.

Perhaps there is something similar that could be used in your VSTO interaction scenario (AFAIR, IDTExtensibility2 ).

If the interaction scenario includes IPC COM calls (for example, when you pass a managed event receiver object to a COM server outside the processor, such as Excel), there is another option for tracking external references to the managed object: IExternalConnection interface . IExternalConnection::AddConnection / ReleaseConnection very similar to IUnknown::AddRef / Release , but is called when a link is added from another COM apartment (including apartments located in separate processes).

IExternalConnection provides a way to implement an almost universal stop mechanism for out-of-turn scripts. When the external reference count reaches zero, you must call ReleaseComObject on any external Excel objects that you can reference, effectively breaking any potential cyclic COM links between the process and the Excel process. It’s possible that something similar has already been implemented in the VSTO runtime (I don’t have much experience with VSTO).

However, if there is no explicit shutdown mechanism, I do not call ReleaseComObject . Also, I never use FinalReleaseComObject .

+1
source

You should not ignore it if you work with the Office GUI! Like your second link state:

Every link you make to a COM object must be released. If you do not, the process will remain in memory.

This means that your objects will remain in memory if you do not explicitly free them. Because they are COM objects, the garbage collector is responsible for freeing them. However, Excel and other fancy tools are implemented without knowledge of the garbage collector in .NET. They are associated with deterministic memory release. If you request an object from excel and don’t release it properly, your application may not close correctly because it is waiting for your resources to be released. If your objects live long enough to get into gen1 or gen2, it can take several hours or even days!

All considerations that COM objects are not being released are aimed at multi-threaded scripts or scripts in which you are forced to move many com objects to multiple instances. As a good tip, always create your com objects as long as possible and release them as soon as possible in the reverse order than the created one. You should also consider keeping your interop instances private, where possible. This reduces the likelihood that other threads or instances will access the object while you have already released it.

0
source

Source: https://habr.com/ru/post/971107/


All Articles