Original Post
I have read a lot stuf of how to implement Dispose(). But there is one thing, that I cannot understand. I have a class A. Class B and C contain the same object of this class (they are not inherit from A). Class A implements the Dispose() method. So i have to implement it in class B and C, too, to give the developer the ability to dispose the helded resources. Now my question: When I dispose B, A will be disposed, too. But C is still holding a reference to A, which is now disposed. What am I doing wrong? here some code, it is just a sample of that what i mean. ///////////////////////////////////////////////////// using System; using someOtherAssembly; static class Program { static void Main() { A parent = new A(); B child1 = new B(parent); C child2 = new C(parent); child1.Dispose(); // now child2 cannot be used anymore, because 'parent' was disposed, too. } } class A : IDisposable { bool isDisposed; IntPtr someUnmanagedResource; public A() { CreateUnmanagedResource(someUnmanagedResource); } ~A() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected void Dispose(bool isDisposing) { if (isDisposed) return; if (isDisposing) { // no managed resources to dispose, so do nothing } FreeUnmanagedResource(someUnmanagedResource); } } class B : IDisposable { bool isDisposed; A objectA; public B(A parent) { objectA = A; } ~B() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected void Dispose(bool isDisposing) { if (isDisposed) return; if (isDisposing) { objectA.Dispose(); } } } class C : IDisposable { bool isDisposed; A objectA; public C(A parent) { objectA = A; } ~C() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected void Dispose(bool isDisposing) { if (isDisposed) return; if (isDisposing) { objectA.Dispose(); } } } ///////////////////////////////////////////////////// When I would create a copy there wouldn't be the problem anymore. But in my real project, class A hold very much more data, a copy would cost too much memory. Thanks in advance.