idisposable

C# On Quit WebPage Delete Files and Folders on Server with no user action

Hi, I have some problems to delete temporary folder and files on my server when users not finish some action in webpages and quit to other webpages. Initialy at Page Load folders are created to allow the user to load files.I have tried implementing destruction during Idisposable without success. Could someone point the best method to del...

ASP.NET web services leak memory when (de)serializing disposable objects?

In the following two cases, if Customer is disposable (implementing IDisposable), I believe it will not be disposed by ASP.NET, potentially being the cause of a memory leak: [WebMethod] public Customer FetchCustomer(int id) { return new Customer(id); } [WebMethod] public void SaveCustomer(Customer value)...

Implementing Dispose on a class derived from Stream

I'm building a class that derives from Stream to wrap a COM IStream. However I've come across an issue where I need to release the COM IStream deteministically. Ok so that's easy just use Marshal.ReleaseComObject in the Dispose method. However I'm not sure its that simple. The Stream base class already has an protected virtual metho...

C# WebPages : manage Exit events or quit with no user action server side

Hi, What is the better solution to manage Exit or Quit events when user exit to other pages with no action?. This event need to be raised only one time and be usefull to delete all temporary files or datas not stored after the abandon of this page. Is a perfect to use as cleaning method server side. Thanks. ...

Is There a Time at which to ignore IDisposable.Dispose?

Certainly we should call Dispose() on IDisposable objects as soon as we don't need them (which is often merely the scope of a "using" statement). If we don't take that precaution then bad things, from subtle to show-stopping, might happen. But what about "the last moment" before process termination? If your IDisposables have not been...

How to implement IDisposable properly

Hi, I've seen so much C# code in my time as a developer that attempt to help the GC along by setting variables to null or calling Dispose() on classes (DataSet for example) within thier own classes Dispose() method that I've been wondering if there's any need to implement it in a managed environment. Is this code a waste of time in it...

Lifetime issue of IDisposable unmanaged resources in a complex object graph?

This question is about dealing with unmanaged resources (COM interop) and making sure there won't be any resource leaks. I'd appreciate feedback on whether I seem to do things the right way. Background: Let's say I've got two classes: A class LimitedComResource which is a wrapper around a COM object (received via some API). There ...

ObjectDisposedException from core .NET code

I'm having this issue with a live app. (Unfortunately this is post-mortem debugging - I only have this stack trace. I've never seen this personally, nor am I able to reproduce). I get this Exception: message=Cannot access a disposed object. Object name: 'Button'. exceptionMessage=Cannot access a disposed object. Object name: 'Button'....

Disposing a Bitmap through its Finalizer

I have a complex program in which I have to first create, then use wrappers around bitmaps and send them across a lot of different classes. The problem in the end is deciding which classes should dispose the bitmaps. Most of the time the end classes don't know if they can indeed dispose the bitmap as the same bitmap can be used in severa...

return the variable used for using inside the using C#

Hello, I am returning the variable I am creating in a using statement inside the using statement (sounds funny): public DataTable foo () { using (DataTable properties = new DataTable()) { // do something return properties; } } Will this Dispose the properties variable?? After doing this am still getting th...

Should I call Dispose() within a function after Return?

Should I call .Dispose() after returning an object that implements IDisposable? myDisposableObject Gimme() { //Code return disposableResult; disposableResult.Dispose(); } In other words, is the object I return a copy, or is it the object itself? Thanks :) ...

Getting rid of nested using(...) statements

Sometimes I need to use several disposable objects within a function. Most common case is having StreamReader and StreamWriter but sometimes it's even more than this. Nested using statements quickly add up and look ugly. To remedy this I've created a small class that collects IDisposable objects and disposes of them when it itself is di...

Should I implement IDisposable here?

My method which calls SQL Server returns a DataReader but because of what I need to do - which is return the DataReader to the calling method which resides in a page code-behind - I can't close the connection in the class of the method which calls SQL server. Due to this, I have no finally or using blocks. Is the correct way of disposi...

Actual use of finally block

I asked to my friend about this question, he said that it is used for destroying the object created during the exception handling. But in c# GC is there for destroying such kinds of unused objects, then what is the actual use of finally block. Tell me with a scenario related to that. ...

Deriving from a component and implementing IDisposable properly

I have a Visual Studio 2008 C# .NET 2.0 CF project with an abstract class derived from Component. From that class, I derive several concrete classes (as in my example below). But, when I go to exit my Form, though the Form's Dispose() member is called and components.Dispose() is called, my components are never disposed. Can anybody sugg...

Disposable Registry: good pattern?

Imagine a: public class Global : IDisposable { private static readonly List<IDisposable> Disposables = new List<IDisposable>(); public void ApplicationStart() { var heavyLifter = new HeavyLifter(); Disposables.Add(heavyLifter); // register a few more } public void Dispose() { ...

How do i return a StringWriter When I cant Clone it?

My code looks like the below. Obviously i cant write 'ok' because the object has been disposed. I cant do return sw.Clone() bc clone doesnt exist. If i dont use a using then at any point between = new and return (like iterating and writing to the object as my example doesnt do) can have an exception and thus not disposing the object. Am...

How do I unit test a finalizer?

I have the following class which is a decorator for an IDisposable object (I have omitted the stuff it adds) which itself implements IDisposable using a common pattern: public class DisposableDecorator : IDisposable { private readonly IDisposable _innerDisposable; public DisposableDecorator(IDisposable innerDisposable) { ...

Can't Use Generic C# Class in Using Statement

I'm trying to use a generic class in a using statement but the compiler can't seem to treat it as implementing IDisposable. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Objects; namespace Sandbox { public sealed class UnitOfWorkScope<T> where T : ObjectContext, IDisposable,...

Should Linq to SQL repository implement IDisposable

I've been googling a ton on repository patterns with Linq over the last few days. There's a lot of info out there but it's often contradictory and I'm still looking for a definitive source. One of the things I'm still not sure about is whether the repository should instantiate it's own DataContext and have a SubmitChanges method, or if ...