Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class "Disposeable" which passes the object to another class "DisposeObject" which inherits "IDisposable".

But, i couldn't able to dispose the object. Do i miss anythings here.

here is my code

C#
public class Disposeable
    {
        List<object> objDisposableObject;

        public Disposeable(List<object> objDisp)
        {
            objDisposableObject = objDisp;
        }

        public void TestDispose()
        {
            int test = objDisposableObject.Count;

            if (test > 0)
            {
                foreach (var objName in objDisposableObject)
                {
                    Console.WriteLine(objName);
                    DisposeObject obj = new DisposeObject(objName);
                    obj.Dispose();
                }
            }
        }
    }

public class DisposeObject : IDisposable
    {
        bool disposed = false;
        object objToDispose = null;
        public DisposeObject(object objInstance)
        {
            objToDispose = objInstance;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    //dispose managed resources
                    /* Here i'm not able to Dispose the object 
                       i.e. objToDispose.Dispose(); */
                }
            }
            //dispose unmanaged resources
            //objToDispose = null;
            disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(objToDispose);
        }
    }

public class Program
    {
        //Program objProgram = new Program();
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.FetchObjects(obj);
        }

        void FetchObjects(object objProgram)
        {
            List<object> lstTest = new List<object>();
            lstTest.Add(objProgram);
            Disposeable objDisposeable = new Disposeable(lstTest);
            objDisposeable.TestDispose();
        }
    }


Code is executing as i expected. but, not able to achieve what i expected.

I request you to guide me with this.

Thank you,
Sridharan
Posted
Updated 11-Jun-15 2:23am
v2

The fact that your parent object DisposeObject implements IDisposable doesn't mean it can then dispose anything that it references internally. The "objToDispose" can only be disposed if *it* implements IDisposable as well.

C#
protected virtual void Dispose(bool disposing)
{
    if (!disposed)
    {
        if (disposing && objToDispose is IDisposable)
        {
            ((IDisposable)objToDispose).Dispose();
        }
    }
    //dispose unmanaged resources
    //objToDispose = null;
    disposed = true;
}


C#
void FetchObjects(object objProgram)
{
    List<object> lstTest = new List<object>();
    // program is not disposable
    lstTest.Add(objProgram);
            
    // SqlConnection is disposable
    lstTest.Add(new SqlConnection());

    Disposeable objDisposeable = new Disposeable(lstTest);
    objDisposeable.TestDispose();
}
 
Share this answer
 
Comments
Sri India 11-Jun-15 8:41am    
Thnak you very much, F-ES.

is there any other way to achieve what i expected?
F-ES Sitecore 11-Jun-15 8:45am    
Not really, IDispoable is an interface that tells calling code that the object has non-managed resources that can't be released automatically by the .net framework, so it has custom code inside it to release those resources itself. If an object has no such resources it does not need to have Dispose called, so it doesn't provide a Dispose method. The Dispose method is only there if the object does implement IDisposable, not all objects implement IDisposable so not all objects can be disposed. When dealing with a range of various objects, you always need to check if the object implements IDisposable before trying to dispose it, whis is what the mofidied code does when it checks "is IDispoable"
This code does not make sense. Why on earth would you create another object for the sole purpose of destroying another object?

I don't understand why you would do complicated code when it could be very simple if you were using a List<IDisposable> instead.

Although there migth be some sensible usage of that pattern, I think it is too advanced for you at that point given the question and the poor implementation.

Before doing advanced stuff you have to learn the basic. Otherwise, your application might just crash and you won't understand why!
 
Share this answer
 
Comments
Sri India 20-Jun-15 7:14am    
Thank you, Philippe.
I thought to keep the disposing logic at a class and to be used in other places.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900