none
Memory leak with Hashtable bucket RRS feed

  • Question

  • Hi,
    I am working on finding memory leaks in my application.
    I have a hashtable which has elements of type List<IMxCallBack> (where IMxCallBack is native interface).I found that the IMxCallback objects in each list element of hashtable are not disposed.I successfully disposed them and found that there are removed from the heap as well in the leak Dump taken on my application.
    But however I found that there are Hashtable objects still on the heap, also the same number of Hashtable+bucket[] objects on heap.But I have disposed the hashtable in dispose as well.
    An aaCalConnection obj is created for every click on my window(which switches to another window).Upon click for every 3 seconds the existing aaCalConnection obj is disposed and hence the hashtable as well.And again a new aaCalConnection object is created which inturn creates a new Hashtable.Please find the code below.

    public aaCalConnection(CONNECTIONTYPE ConnType, string Node, string Application, string Topic, aaDataSubscriptionManager Callback, bool isLocalConnection, aaAccessName accessname, aaCalConnectionManager CalMgr)
        {
          -------------
          ------------
    
          m_CalbackCache = new Hashtable();
    
    }
    
    
    void AddCallbackToCache(int datasourceId, IMxCallback callback)
    		{
    			//Add this callback to the queue of pending writes
    			List<IMxCallback> callbackList = m_CalbackCache[datasourceId] as List<IMxCallback>;
    			if (callbackList == null)
    			{
    				callbackList = new List<IMxCallback>();
    				m_CalbackCache[datasourceId] = callbackList;
    			}
    			if (callbackList != null)
    			{
    				callbackList.Add(callback);
    			}
    		}
    
    
    public void RemoveCallbackFromCache(int datasourceId, IMxCallback callback) 
    		{
    			//Remove this callback from the queue of pending writes
    			List<IMxCallback> callbackList = m_CalbackCache[datasourceId] as List<IMxCallback>;
    
    			if (callbackList != null)
    			{
    				callbackList.Remove(callback);
    
        		    if (callbackList.Count == 0)
    	        	  m_CalbackCache[datasourceId] = null;
    
    		        callbackList = null;
    
    			}
    		}
    
    
    public virtual void Dispose()
        {
    			---------------
    			----------
    				m_CalbackCache.Clear();
    				m_CalbackCache = null; 
    
         }
    
    // Disposing the IMxCallback object which is a userconnection callback object from another class
    
    m_CalConnection.RemoveCallbackFromCache(callback.DataSourceID, callback);
    Callback.Dispose();

    The managed and unmanaged cleanoffs are properly done.I don't see any of those objects on heap .Now while debugging the code in dispose I could notice that eventhough I have cleared the hashtable.The buckets in hashtable are not cleared.I even assigned null to hashtable after clearing it but still there are hashtable and hashtable bucket objects in the heap.Can anyone tell me why they are not Garbage collected.How should i go about resolving this leak?

    Thanks
    Satya

    Friday, April 15, 2011 1:37 PM

Answers

  • Does your item which is not being cleaned uphave any event subscriptions it subscribes to or any on it which other objects subscribe to. Objects can become pinned if they contain unsubscribed events. Make sure all events are unsubscribed. HTH
    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by Paul Zhou Monday, April 25, 2011 3:29 AM
    Saturday, April 16, 2011 12:53 AM
    Moderator
  •  

    Hi satkrish,

     

    Dispose method is used to close native resource handles. You need to call GC.Collect to collect managed objects in GC heap.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Monday, April 25, 2011 3:29 AM
    Wednesday, April 20, 2011 8:44 AM

All replies

  • Does your item which is not being cleaned uphave any event subscriptions it subscribes to or any on it which other objects subscribe to. Objects can become pinned if they contain unsubscribed events. Make sure all events are unsubscribed. HTH
    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by Paul Zhou Monday, April 25, 2011 3:29 AM
    Saturday, April 16, 2011 12:53 AM
    Moderator
  •  

    Hi satkrish,

     

    Dispose method is used to close native resource handles. You need to call GC.Collect to collect managed objects in GC heap.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Monday, April 25, 2011 3:29 AM
    Wednesday, April 20, 2011 8:44 AM