I added a reference in a Visual Studio project to some COM library. Now when I create and object in a method, will be this COM object automatically released when program leaves that method? Or should I always release these objects manually? (ReleaseComObject or FinalReleaseComObject?).
-
1There is more on this [here](http://stackoverflow.com/questions/1827059/why-use-finalreleasecomobject-instead-of-releasecomobject) – StuartLC Sep 26 '12 at 07:23
-
1Spare some time to investigate, and be patient as the articles are all tough and boring, such as http://blogs.msdn.com/b/visualstudio/archive/2010/03/01/marshal-releasecomobject-considered-dangerous.aspx – Lex Li Sep 26 '12 at 07:29
2 Answers
It's certainly not done automatically, and in some cases it can be dangerous to do so (by calling Marshal.ReleaseComObject
) as described in this blog post.
For In-Proc COM objects, there's often no need to release COM objects at all.
For Out-Proc COM objects, it can be important to release them, to prevent problems such as Office apps failing to shut down after automation from a .NET client.
In this case, I'd follow the advice in the above linked blog post:
If you are using a COM object in a scoped, single-threaded manner then you can safely call ReleaseComObject on that object when you are done with it.
But if you are using a COM object from multiple places or multiple threads in your application (or from other applications in the same process), you should not call ReleaseComObject

- 122,218
- 32
- 205
- 338
You can use Marshal.ReleaseComObject: http://msdn.microsoft.com/de-de/library/system.runtime.interopservices.marshal.releasecomobject.aspx

- 5,918
- 3
- 47
- 86