how to dispose session variables automatically

Discussion in 'ASP .Net' started by Andy Fish, Aug 26, 2008.

  1. Andy Fish

    Andy Fish Guest

    Hi,

    I have some objects that implement IDisposable and they in turn need to
    dispose of other objects (Only managed resources are being used).

    My objects are held in the HTTP session. Is there a way to get these to be
    Disposed properly when the session ends?

    I guess I could use the session_end event but apparently it's not a good
    idea to rely on this because it might not be called in all circumstances.
    Also it's not possible to call dispose of another managed object from inside
    a finalizer so I can't do it there.

    is there an accepted best practice for this?

    Andy
     
    Andy Fish, Aug 26, 2008
    #1
    1. Advertising

  2. "Andy Fish" <> wrote in message
    news:...
    > Hi,
    >
    > I have some objects that implement IDisposable and they in turn need to
    > dispose of other objects (Only managed resources are being used).
    >
    > My objects are held in the HTTP session. Is there a way to get these to be
    > Disposed properly when the session ends?
    >
    > I guess I could use the session_end event but apparently it's not a good
    > idea to rely on this because it might not be called in all circumstances.
    > Also it's not possible to call dispose of another managed object from

    inside
    > a finalizer so I can't do it there.
    >
    > is there an accepted best practice for this?
    >


    Yeah don't store IDisposable objects in the session ;)

    Seriously though first consider whether you really need to be holding
    disposable types. If you need to hold these types it will be because they
    internally hold something disposable can those internal references be
    disposed before the end of a request?

    How unreliable do you think Session_End is? If occasionally it doesn't run
    is it a disaster that sometimes things have to wait until a GC run to be
    tidy up? After all its likely the session has been hanging around idle for
    a while anyway.


    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Aug 26, 2008
    #2
    1. Advertising

  3. Andy Fish

    Hans Kesting Guest

    After serious thinking Anthony Jones wrote :
    >
    > How unreliable do you think Session_End is?


    Apparently if you use something other than InProc storage for the
    session, the Session_End is never called.

    Hans Kesting
     
    Hans Kesting, Aug 27, 2008
    #3
  4. "Hans Kesting" <> wrote in message
    news:...
    > After serious thinking Anthony Jones wrote :
    > >
    > > How unreliable do you think Session_End is?

    >
    > Apparently if you use something other than InProc storage for the
    > session, the Session_End is never called.
    >


    Umm.. ok. are you using Out of Proc storage?

    Is it possible to store objects that truely need to be disposed (due to
    using an unmanaged resource) in an out-of-proc store?

    There are three nominal ways a session ends, it times out due to being idle,
    a deliberate user action or if its inproc the process is a victim of
    recycling.

    If it ends on idle does it really matter that objects aren't immediately
    disposed?
    If the process terminates naturally then the objects are dropped and any
    unmanaged resources would do what they naturally do when a process ends.

    If a deliberate user action then code on the server is needed to bring that
    about, you could place code there to handle the disposing. Thats pretty
    messy.

    Like I said you really want to avoid disposable objects being stored in the
    session in the first place

    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Aug 27, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SS
    Replies:
    2
    Views:
    455
    Trond-Eirik Kolloen
    Jul 3, 2003
  2. Bob
    Replies:
    1
    Views:
    417
    Nick Harris
    Jan 21, 2004
  3. Antonio Concepcion
    Replies:
    3
    Views:
    2,737
    Antonio Concepcion
    Feb 17, 2005
  4. Big George

    Dispose variables to free memory

    Big George, Sep 6, 2010, in forum: ASP .Net
    Replies:
    1
    Views:
    908
    Cubaman
    Sep 7, 2010
  5. JP SIngh
    Replies:
    1
    Views:
    406
    Evertjan.
    Aug 9, 2004
Loading...

Share This Page