0

I have the following class:

public class test
{
       public int datacapturecount { get; set; }
       public int sno { get; set; }
       public string name { get; set; }
       public string sourceaddr { get; set; }
       public string destaddr { get; set; }
       public string Bssid { get; set; }
       public string packetsubtype { get; set; }
       public UInt16 datarate { get; set; }
       public SByte signal { get; set; }
       public UInt32 channel { get; set; }
       public UInt32 size { get; set; }
}

I am using this class while adding rows to wpf datagrid In a infinite loop:

for(I=0; ; I++)
{
    datagrid.Items.Add(new test() {
        datacapturecount = 0, 
        sno = I,
        name = "ssss",
        sourceaddr = "44545454"
    }); 
}

Here the class test is creating multiple instances and causing memory leaks. How to avoid this? How to Write destructor for above class so that I can force the GC to collect the memory immediately after adding the row to datagrid?

CharithJ
  • 46,289
  • 20
  • 116
  • 131

4 Answers4

4

There is no memory leak. Each class instance is being added to datagrid.Items, so all the time you keep a reference to datagrid.Items, the references to test instances have to remain in memory. Because you have an infinite loop around adding items, the code will run until it runs out of memory.

Adding a destructor or IDisposible to this class will have no affect. You need to manage the number of items you add to datagrid.Items.

David Arno
  • 42,717
  • 16
  • 86
  • 131
2

There is no memory leak there. Simply you are trying to add an infinite number of rows to a DataGrid. Before or later you will exhaust the memory.

for(I=0; ; I++)

There is no check for ending the for cycle:

for(I=0; I < 100; I++)

This will try adding 100 rows.

xanatos
  • 109,618
  • 12
  • 197
  • 280
0

As mentioned in the previous answers your code will not result in any memory leaks.

If you still want to add a destructor to your class (for other reasons) you can do so by adding the following to your class.

~test()
{
    // Do whatever cleanup you find necessary.
}
Markus
  • 767
  • 2
  • 7
  • 19
  • I'm not sure why this is downvoted as the title in the question clearly reads "How to write a destructor for a class?" and that is exactly what I'm answering. – Markus May 20 '15 at 12:05
0

How to add a destructor for a class

When to implement IDispose

You should implement IDisposable pattern if you have any class variables that have implemented IDispose interface. Simply if you have any class variables those have Dispose method, you should implement IDispose interface and Dispose all disposable objects.

When to implement Finalizer (Destructor)

You should implement Finalizer(or destructor) only if you have any unmanaged resources in your class. See above msdn page for more details. You should not implement finalizer if you don't have any unmanaged resources. Because, having the finalizer causes those instances to stay longer in memory. In fact, it will stay in memory for two GC cycles. First GC cycle add it to the CLR finalization queue and the second GC cycle would actually dispose it. That's why you suppress finalization in the Dispose method when you manually call it.

Go back to your code

Apparently there are no any Disposable class variables available in your test class. So, you don't have to implement IDisposable in this Test class at all. Infinite loop could be the reason for your concern.

CharithJ
  • 46,289
  • 20
  • 116
  • 131