CRK
CRK

Reputation: 607

Dispose instance

Class1:

      public class FunctionBlocks
      { 
          List<Hashtable> _htLogicalNodeList;   
          public FunctionBlocks()
          { 
            _htLogicalNodeList = new List<Hashtable>();
            FunctionBlock fb = new FunctionBlock();
            fb.AddDODASignalList(new Hashtable);            
            _htLogicalNodeList.Add(fb.LogicalNodeHash);     
            fb = null;
          }     
      }

Class2:

      public class FunctionBlock
      {
        Hashtable _htLogicalNode;

        public FunctionBlock()
        {
            _htLogicalNode = new Hashtable();
        }

        public Hashtable LogicalNodeHash
        {
            get{return _htLogicalNode;}
            set{_htLogicalNode = value;}
        }

        public void AddDODASignalList(Hashtable doDASignal)
        {
            _htLogicalNode.Add(doDASignal);
        }
     }

In this example I wan't to dispose "_htLogicalNode" . "fb" object I have make it as null ,Eventhough "FunctionBlocks" instance have "_htLogicalNode" references. How I can dispose "_htLogicalNode" instance.

Upvotes: 0

Views: 2427

Answers (3)

Justin
Justin

Reputation: 86799

What do you mean by "dispose"? You could have FunctionBlock implement IDisposable in which case you could use the following:

using (FunctionBlock fb = new FunctionBlock())
{
    fb.AddDODASignalList(new Hashtable);            
    _htLogicalNodeList.Add(fb.LogicalNodeHash);     
}

However I can't see anything in FunctionBlock that needs disposing, and so doing this would be pointless - the IDisposable interface / pattern is essentially just a fancy / robust way of calling a method when you are done with an object. Unless you do something in the implemented Dispose method then this does nothing.

If by "dispose" you mean free up the memory then the answer is that you don't need to do anything (you don't even need to set fb to null). Simply let fb go out of scope and the garbage collector will collect it and free up the used memory in its own time.

You may find that the memory used by fb is not freed immediately - this is perfectly normal and to be expected. There are ways of forcing the garbage collector into doing "its thing" when you want it to, but doing this is very bad practice.

Upvotes: 0

Zenwalker
Zenwalker

Reputation: 1919

Override dispose method and there you can make it null

Upvotes: 0

Related Questions