views:

133

answers:

4

Hello; I am trying to adapt a class to work with just finished that I am not able to solve the problem. My question is how to identify the handle to close / / CloseHandle (handle). My problem is that I am not able to adapt the following code.

for (Int32 i = 0; i < temp_items.Count; i++)
{ 
        string conj_itens = temp_itens[i].ToString();
         ContarSuporte cs= new ContarSuporte(matriz_bin,strExpr);
 _cont = cs.Suporte;
      If (_cont>100) 
   {
     list.add(conj_itens);
   }

} 


public class ContarSuporte: IDisposable 
{
    private int _cont;
    private bool disposed = false;
    private IntPtr handle; 

    public ContarSuporte()
    { 
    } 

   public ContarSuporte(DataTable matriz_binaria, string strExpr)
    {
        int c = matriz_binaria.Rows.Count;
        this._cont = (int)(matriz_binaria.Compute("COUNT(TID)", strExpr)); 
    } 
    ~ContarSuporte()
     {

         Dispose(false);

    } 
    public void Dispose()
    {
        //GC.Collect();
        Dispose(true);
        GC.SuppressFinalize(this); 
        // GC.Collect();
    } 

    private void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                //this.Dispose();
            }
            //CloseHandle(handle); 
            handle = IntPtr.Zero;

        }
        disposed = true;
    } 

    public int  Suporte
    {
        get
        {
            return _cont;
        }
        set
        {
            _cont =value;
        }
    }
A: 

You don't seem to have any handles that need closing? Why do you think you need to call the CloseHandle method?

David M
+1  A: 

You haven't shown any code actually using the handle. Are you really sure you need to implement IDisposable and have a handle in the first place? What unmanaged resource are you trying to use?

Were you perhaps just following a pattern that you saw elsewhere which did use a handle? You should only implement IDisposable when you actually need to - and implementing a finalizer should be very rare these days (as you can use SafeHandle instead of IntPtr for handles).

Jon Skeet
Thanks for the replies, the problem is here, in many passages lopp made by the data, and is creating much garbage in memory that I am not able to control, how can I control this this._cont = (int)(matriz_binaria.Compute("COUNT(TID)", strExpr));
Creating lots of objects in memory isn't at all the same thing as requiring IDisposable or handles. Again: do you have any *unmanaged* resources which you need to dispose of?
Jon Skeet
+2  A: 

What handle are you trying to close? I can't see anything that allocates a handle.

Also - in many cases you can use SafeHandle to make this simpler - I can't tell whether this applies in your case, since I can't see what is going on...

Marc Gravell
A: 

Hello,

the idea is to finish this instruction this._cont = (int)(matriz_binaria.Compute("COUNT(TID)", strExpr)); to each passage, the problem is not to increase the use of the memory I think most is because it is garbage. my idea was to create a class to avoid the growth of memory. Already placed the _cont variable = 1 and had no impact on memory, and the problem that this raises intrucção memory, and the purpose and effect of each passage was interested in cleaning the garbage memory that is created with this instruction this._cont = (int)(matriz_binaria.Compute("COUNT(TID)", strExpr));**

Thanks