Search code examples
c#c++comappdomain

Unloading appdomain don't clear C++ COM object static members


I'm using a 3rd party C++ COM objects which seems to hold a few static members. The thing is I have to reset the static members everytime I'm starting a new run. Therefore I currently need to close and open my application. I tryed to use AppDomain to unload the whole memory of those static members but I have no idea why it won't work. I'm rasing the COM object using Activator.CreateInstance(Type.GetTypeByCLSID) is there anything I need to do manually to dispose those instances? note that I checked and any static C# objects does dispose that way.


Solution

  • Unmanaged resources are called unmanaged because their memory isn't handled by the managed garbage collector.

    Any unmanaged resource must be manually disposed. This is one of the goals (if not the most important one) of IDisposable: its implementations have some associated unmanaged resource. For example, a FileStream needs to be disposed manually because it's a wrapper of Win32 API calls.

    In summary, unloading an AppDomain or any other approach won't help here. You're who needs to code to dispose these COM static resources whenever you don't need them anymore (i.e. when you close the app).