Releasing COM Object in C#
I recently encountered an exception in my latest project System.Runtime.InteropServices.InvalidComObjectException and I wonder what going on with this exception. In my project we used a OCX as reference then VS2008 created a sort of interop classes we can use those classes in our code. The problem is when we try to exit the application this nasty exception occurs. I put a destructor in my user control class but still it occurs. I think this happens because the object reference was already detached before the app was able to call the destructor. So, what I did is create a function in my use control class public void ReleaseCOMObject() { System.Runtime.InteropServices. Marshal .ReleaseComObject( this .m_objCOMobject); } and call this function before I call the app exit. See below, private void btnExit_Click( object sender, EventArgs e) { m_usercontrolObject.ReleaseCOMObject(); Application .Exit(); ...