I have a class that uses a .NET wrapper for a COM object and well it is giving me that infamous RCW error, so in my investigation I found that if I take out the Dispose() method from the finalizer of this class it will fix the RCW error so something is wrong for example the object is getting disposed but the registered events are still hanging around ... But just removing the Dispose() can't be the answer because then who is going to release the memory ? ( I ran a Memory profiler and confirmed that just removing the Dispose method causes like 20MB of extra Unmanaged Memory )
so something should be wrong with the way I am using the Dispose model.. here is what I have:
private MyCOMobject theCOMobject = null;
static SuppressFieldCntrlr()
{
new SomeCalss();
}
~SuppressFieldCntrlr()
{
Dispose(false);
}
private bool disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
theCOMobject.Dispose();
}
MethodFoo(false);
disposed = true;
}
}