none
Disposing of an object RRS feed

  • Question

  • I have a class NewVehicle which extends Vehicle.  I want to dispose NewVehicle object newVehicle.   I need to use IDiposable, but don't where to use it.  I have

    class NewVehicle : Vehicle{......}

    Do I put IDisposable after Vehicle?

    I have this code in NewVehicle

    bool disposed = false;
    
            // Protected implementation of Dispose pattern.
            protected override void Dispose(bool disposing)
            {
                if (disposed)
                    return;
    
                if (disposing)
                {
                    // Free any other managed objects here.
                    //
                }
    
                // Free any unmanaged objects here.
                //
                disposed = true;
    
                // Call the base class implementation.
                base.Dispose(disposing);
            }
    
            ~NewVehicle()
            {
                Dispose(false);
            }

     but getting two errors, Protected Override and base.Dispose(), though not sure if I'm missing code where the comments are.

    Any help or guidance would be appreciated.  

     

    Sunday, December 3, 2017 11:45 AM

All replies

  • See the following post (unless you have seen it already).

    https://stackoverflow.com/questions/12368/how-to-dispose-a-class-in-net


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, December 3, 2017 3:04 PM
    Moderator
  • IDisposable can be put either NewVehicle or Vehicle class. When you implement IDisposable on Base class, this will available in all inherited class. 

    Please see following code snippet  tested this in a console app

    namespace DisposeExample
    {
        class Program
        {
            static void Main(string[] args)
            {
                var newVehicle = new NewVehicle();
                newVehicle.Dispose();     
                Console.Read();
            }
        }
    }
    
    // Base Class 
    public class Vehicle : IDisposable
    {
        public void Dispose()
        {        
        }
    }
    
    public class NewVehicle:Vehicle 
    {
    }



    Sunday, December 3, 2017 8:23 PM
  • I have a class NewVehicle which extends Vehicle.  I want to dispose NewVehicle object newVehicle.   I need to use IDiposable, but don't where to use it.  I have

    class NewVehicle : Vehicle{......}

    Do I put IDisposable after Vehicle?

    I have this code in NewVehicle

    bool disposed = false;
    
            // Protected implementation of Dispose pattern.
            protected override void Dispose(bool disposing)
            {
                if (disposed)
                    return;
    
                if (disposing)
                {
                    // Free any other managed objects here.
                    //
                }
    
                // Free any unmanaged objects here.
                //
                disposed = true;
    
                // Call the base class implementation.
                base.Dispose(disposing);
            }
    
            ~NewVehicle()
            {
                Dispose(false);
            }

     but getting two errors, Protected Override and base.Dispose(), though not sure if I'm missing code where the comments are.

    Any help or guidance would be appreciated.  

     

    IDisposable is pretty confusing, huh? Welcome to my world. I think I'm starting to get the hang of it. I carried it out 3 generations for your review. To see how it works, I suggest you put breakpoints on all the dispose  methods and then create an instance of the child and call child.Dispose().  Technically you can delete all the finalizer methods since I don't have any unmanaged resources. An unmanaged resource is typically an IntPtr obtained as the result of a call to a C++ based Win32 method (i.e. a call to Pinvoke that returns a filehandle, network handle, etc in the form of a raw IntPtr). If you're not using Pinvoke diectly, you probably don't have any such resources - you just need to call Dispose on any regular resources (and trust that THOSE resources will, during that Dispose call, dispose of their own Pinvoked resources). 

    	public class Grandparent : IDisposable {
    		private bool _disposed;
    		public void Dispose(){
    			Dispose(true);
    			GC.SuppressFinalize(this);
    		}
    		protected virtual void Dispose(bool disposing) {
    			if (_disposed) return;
    			if (disposing) {
    				//Call any ManagedObject.Dispose() here.
    			}
    			//Dispose of unmanaged objects here.
    			_disposed = true;
    		}
    		~Grandparent(){
    			Dispose(false); //False signals that the finalizer called Dispose().
    		}
    	}
    
    	public class ParentClass: Grandparent {
    		private bool _disposed;
    		protected override void Dispose(bool disposing){
    			if (_disposed) return;
    			if (disposing){
    				//Call any ManagedObject.Dispose() here.
    			}
    			//Release unmanaged objects here.
    			//Then call base.Dispose(disposing).
    			base.Dispose(disposing);
    			_disposed = true;
    		}
    		~ParentClass(){
    			Dispose(false);
    		}
    	}
    
    	public class Child : ParentClass {
    		private bool _disposed;
    		protected override void Dispose(bool disposing)
    		{
    			if (_disposed) return;
    			if (disposing){
    				//Call any ManagedObject.Dispose() here
    			}
    			//Release unmanaged objects here.
    			//Then call base.Dispose(disposing).
    			base.Dispose(disposing);
    			_disposed = true;
    		}
    		~Child(){
    			Dispose(false);
    		}
    	}

    Here's an example of Pinvoke from Pinvoke.Net. Add this library reference (to this DLL) to your C# code and then call the function which returns a raw IntPtr. 

    [DllImport("gdi32.dll")]
    static extern IntPtr CreateBitmap(int nWidth, int nHeight, uint cPlanes, uint cBitsPerPel, IntPtr lpvBits);



    • Edited by jal2 Monday, December 4, 2017 2:37 AM
    • Proposed as answer by Fei HuModerator Tuesday, December 5, 2017 9:08 AM
    Monday, December 4, 2017 2:29 AM
  • Hi RichardDunneBSc,

    Is there any update? do you resolve the issue? if so, please mark helpful reply as answer or share your solution, it will be beneficial to other communities who have similar issue. if not, please provide more information and feel free let us know.

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 13, 2017 6:46 AM
    Moderator
  • I'll add comment that when writing P/Invoke calls with pointer to unmanaged resources that should be freed by yourself, always use some form of SafeHandle to replace IntPtr there (example here) to make sure the .NET runtime will remember to release it when you do not.

    Wednesday, December 13, 2017 7:04 AM
    Answerer
  • Strange that beside Karen everybody is replying to this if Dispose is the same as Deconstructing in C#.

    Or maybe not so strange as many have read a book about OO and that an constructed object had always to be deconstructed and have shouted that on Internet as dispose. 

    But that is the reason managed languages are created because it was impossible to manage for human programmers in a program bigger than 10 classes. 


    Success
    Cor

    Wednesday, December 13, 2017 12:32 PM
  • Not that strange for me.

    Since the first reply is good answer already, there is no more thing to add for me. So at least for me I'm just commenting on others comment.

    That's how a thread derails. XD

    Thursday, December 14, 2017 1:44 AM
    Answerer