Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session containing items implementing IDisposable

In ASP.NET if items are left in the session state that Implement IDisposable but are never specifically removed and disposed by the application when the session expires will Dispose be called on the objects that any code in Dipose() will execute?

like image 845
Chris Marisic Avatar asked Jan 31 '09 02:01

Chris Marisic


People also ask

Which of the following keywords can you use with an object implementing IDisposable to ensure the object cleans up resources as soon as possible?

You can call the IDisposable. Dispose() method from the finally block of a try/finally statement. By using the finally block, you can clean up any resources that are allocated in a try block, and you can run code even if an exception occurs in the try block.

Why should we implement IDisposable?

in a class, you should implement IDisposable and overwrite the Dispose method to allow you to control when the memory is freed. If not, this responsibility is left to the garbage collector to free the memory when the object containing the unmanaged resources is finalized.

What are IDisposable objects?

Typically, types that use unmanaged resources implement the IDisposable or IAsyncDisposable interface to allow the unmanaged resources to be reclaimed. When you finish using an object that implements IDisposable, you call the object's Dispose or DisposeAsync implementation to explicitly perform cleanup.

What is IDisposable interface in C implement the Dispose method?

The dispose pattern is used for objects that implement the IDisposable interface, and is common when interacting with file and pipe handles, registry handles, wait handles, or pointers to blocks of unmanaged memory. This is because the garbage collector is unable to reclaim unmanaged objects.


1 Answers

I'd disagree with Sean's answer; firstly, finalizers should not be routinely added to classes, even if they are IDisposable - finalizers should only really be used in classes that represent unmanaged resources. Conversely, a class with a finalizer often is also IDisposable.

Re the question: is Dispose() called - no, it isn't. The object will be garbage collected at some point in the future (indeterminate), but that is about it. A finalizer wouldn't add much here, as any encapsulated objects will also already be eligible for collection (assuming that they aren't referenced elsewhere).

like image 51
Marc Gravell Avatar answered Oct 22 '22 13:10

Marc Gravell