0
 public struct LocalTLS : IDisposable
{
    public byte[] bArr;
    public string str;
    public LocalTLS(int k)
    {
        str = "Labamba";
        bArr = new byte[20000];
    }
    public void Dispose()
    {
        MessageBox.Show("In Dispose");
        //Thread.FreeNamedDataSlot("jaja");
        int k = 90;
        k += 90;
    }
} 
private void buttonTLS_retreive_Click(object sender, EventArgs e)
{
        LocalTLS lts = new LocalTLS(1);
}

After the click event returns I expect the Dispose call but THAT NEVER HAPPENS, Can Some one explain. I do not want to do any manual steps like calling Dispose or do using. I want that to happen automatically.

Well thanks for all your answers, I wanted to implement a a class for LocalDataStoreSlot, that will automatically free the Slot and it goes out of scope, as shown in the commented code. This will free the developer from remembering to call dispose, wherein the actual FreeNamedDataSlot(...) happens. Seems like that is not possible

Dr.sai
  • 285
  • 3
  • 4
  • 3
    Here's my only follow up question: do you know when and how to use `IDisposable`? [Here's a link, just in case](http://msdn.microsoft.com/en-us/library/system.idisposable.aspx) – Patryk Ćwiek May 09 '14 at 12:44
  • 1
    could you explain why you expect the Dispose method to be called after the event returns? – Ethan Cabiac May 09 '14 at 13:00
  • *I expect the Dispose call but THAT NEVER HAPPENS* - intended behavior, your expectations are simply wrong. =D – Sinatr May 09 '14 at 13:19
  • Dispose() is not called automatically when lts goes out of scope. Either call it manually or wrap it in a "using" block. – Vasile Mare May 10 '14 at 03:36

3 Answers3

1

The CLR does not perform garbage collection all the time, otherwise your program will be very slow because it is performing GC. To perform GC, the CLR has to suspend ALL the threads before reclaiming memory used by out of scope objects. That is a very big performance impact.

The CLR will perform GC and call the object's Dispose method when necessary. Do not call the object's Dispose() method unless you have a good reason. If the object is associated to native system handles, network connections, file handles etc., those are good reasons to use the using keyword or call it's Dispose() method. Generally the CLR will GC objects that are out of scope fast enough such that your application will not run out of memory.

I understand your concern, you believe that byte[20000] is a significant memory footprint to your application and you want to control your memory usage carefully. But this is not C++. The CLR will automatically manage memory for you. In my past 7 years of .NET develop I have only come across one application where the default GC is not fast enough and I have to call Dispose() on objects manually. So in short, just hand over memory management to the CLR - it does a very good job.

p.s. if you're interested in how GC works internally, there are a number of in-depth articles online. Just search - you'll find them.

kevin
  • 2,196
  • 1
  • 20
  • 24
0

You've to wrap your object declaration inside using like this:

using(LocalTLS lts = new LocalTLS(1))
{
   // use lts here

} // at this point, the Dispose method is called.

or call Dispose method manually:

lts.Dispose();

Also, the Dispose() method is automatically called when there is no reference to lts in the code, by the Garbage Collector.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • What if I do not want to use using as I could be modifying values – Dr.sai May 09 '14 at 12:47
  • @Dr.sai, see the last line of my answer for that – Amit Joki May 09 '14 at 12:48
  • Nor do I want to call dispose, If Microsoft say they will be automatically collected then I expect a call on the Dispose, Thanks – Dr.sai May 09 '14 at 12:48
  • Nope dispose is never called as that message box never comes – Dr.sai May 09 '14 at 12:52
  • @Dr.sai that's what Amit was trying to tell you - it won't be called. You need to use a `using` block or call it yourself. That's *just how it works*. It is not a destructor. – Steve May 09 '14 at 13:47
0

Its a struct, it lives on the memory stack not the memory heap. So its not going to be collected by the garage collector like objects do, so the GC will not call the dispose method. Your struct "LocalTLS " will be freed up and taken off the memory stack when your method ends. When it does this, it does not look for the IDisposable interface.

RussellEast
  • 153
  • 8