none
Dispose mySite in using (SPSite mySite = SPContext.Current.Site)? RRS feed

  • Question

  • I've checked my code with the SPDispose Check Utility (no errors) and I'm writing something like

    using (SPSite mySite = SPContext.Current.Site)
    {
        ...
    }

    Stefan Goßer writes in his blog (http://blogs.technet.com/stefan_gossner/archive/2008/12/05/disposing-spweb-and-spsite-objects.aspx):

    "
    Using "using" statements is a very nice method to ensure that at the end the object being used gets properly disposed. The problem in the code above is that SPContext.Current.Site and SPContext.Current.Web are not allowed to be disposed! Using successfully hides the dispose statement here. "Using" allows a nice structuring of the code so users often use it without thinking about the side effects."

    Is this statement correct? I've seen this coding practice a lot of times in the web.

    Is the better solution to create the SPSite object with the url of the context spsite?

    using (SPSite mySite = new SPSite(SPContext.Current.Site.Url))
    {
        ...
    }
    • Moved by Mike Walsh FIN Tuesday, April 21, 2009 2:16 PM MySite q
    • Moved by Mike Walsh FIN Tuesday, April 21, 2009 3:10 PM not MySite but a self-named mySite !
    Tuesday, April 21, 2009 1:58 PM

Answers

  • The SPSite and SPWeb objects associated with SPContext.Currrent {Site | Web} are shared in the HttpContext of the page.
    I have seen MANY MANY times where people have disposed of SPContext.Current.Web or SPContext.Current.Site .. and another webpart on the page has broken!

    So in answer to your question:

    You should not dispose the SPSite.RootWeb IF you have gotten your SPSite object from SPContext.Current.Site!

    The best practice method would be:

    using (SPSite mySite = new SPSite(SPContext.Current.Site.Url))
    {
        ...
    }


    This will create a brand new SPSite object (from the "new" constructor).

    I would also recommend that you don't use SPSite.RootWeb, instead use:

    SPWeb rootWeb = SPSite.OpenWeb(SPSite.RootWeb.ID);

    Again, this creates a brand new SPWeb object. You don't need to dispose of this manually, the SPSite "using" statement will do that for you.



    regards
    Martin Hatch
    MCPD .Net Web Development
    MCTS WSS 3.0 | MOSS 2007
    Visit my Blog
    • Marked as answer by Mike Walsh FIN Wednesday, April 22, 2009 9:24 AM
    Wednesday, April 22, 2009 9:02 AM

All replies

  • MySite questions as always go to Social Computing.

    Moving this.



    WSS FAQ sites: WSS 2.0: http://wssv2faq.mindsharp.com WSS 3.0 and MOSS 2007: http://wssv3faq.mindsharp.com
    Total list of WSS 3.0 and MOSS 2007 Books (including foreign language titles) http://wssv3faq.mindsharp.com/Lists/v3%20WSS%20FAQ/V%20Books.aspx
    Tuesday, April 21, 2009 2:16 PM
  • Totally correct.

    You should not dispose of any "SPContext.Current" objects .. as you will be disposing a shared context object.
    The best practice is to create a new method (either from a constructor method .. or another method for SPWeb objects such as OpenWeb)

    General Rule of Thumb:

    * If you are getting it from a property, do NOT dispose
    * IF you are getting it from a method / constructor then ALWAYS dispose.


    regards
    Martin Hatch
    MCPD .Net Web Development
    MCTS WSS 3.0 | MOSS 2007
    Visit my Blog
    • Proposed as answer by Martin Hatch Tuesday, April 21, 2009 2:23 PM
    • Unproposed as answer by Mike Walsh FIN Wednesday, April 22, 2009 9:24 AM
    Tuesday, April 21, 2009 2:18 PM
  • Hi Diana,

    I do exactly what you say in your code.  It doesn't seem to have much of an overhead and I can gurantee that I dipose of what I use.

    I also read that you should dispose of the RootWeb object whenever you use the SPSite.

    using (SPSite mySite = new SPSite(SPContext.Current.Site.Url))
    {
        mySite.RootWeb.Dispose();
    }

    • Proposed as answer by RichieH Tuesday, April 21, 2009 2:20 PM
    Tuesday, April 21, 2009 2:20 PM
  • MySite questions as always go to Social Computing.

    Moving this.



    WSS FAQ sites: WSS 2.0: http://wssv2faq.mindsharp.com WSS 3.0 and MOSS 2007: http://wssv3faq.mindsharp.com
    Total list of WSS 3.0 and MOSS 2007 Books (including foreign language titles) http://wssv3faq.mindsharp.com/Lists/v3%20WSS%20FAQ/V%20Books.aspx

    Mike .. this isn't a "MySite" question .. it's a general SPSite / Dispose question.
    I think there was confusion because she called her variable "mySite" (as in .. an SPSite object that belongs to me).


    regards
    Martin Hatch
    MCPD .Net Web Development
    MCTS WSS 3.0 | MOSS 2007
    Visit my Blog
    Tuesday, April 21, 2009 2:23 PM
  • Thanks Martin. I hate it when people do that ...

    There was another post only a few minutes later where my site definitely meant MySite (because it also talked about personal site) so I can't assume that MySite = MySite and my site doesn't.

    Add to that the amount of posts I look at and the time per post and this happens now and again.

    Thanks for the message - moving back to Dev/Programming -luckily thanks to a post being marked as Propose as Answer, I saw this (that and the fact that I wondered at the time ...).

    Mike

    WSS FAQ sites: WSS 2.0: http://wssv2faq.mindsharp.com WSS 3.0 and MOSS 2007: http://wssv3faq.mindsharp.com
    Total list of WSS 3.0 and MOSS 2007 Books (including foreign language titles) http://wssv3faq.mindsharp.com/Lists/v3%20WSS%20FAQ/V%20Books.aspx
    Tuesday, April 21, 2009 3:09 PM
  • Sorry for the confusion of mySite, I actually didn't ment "My Site" ;)

    Just to clarify: Does using this statement

    using (SPSite theSite = SPContext.Current.Site)
    {
        ...
    }

    result in memory problems, due to the fact that I can't dispose theSite or does the automatically dispose mechanisms of SharePoint care for this reference? If it really does produce memory problems, I'd suggest to include this bad code practice (that I've seen a lot of times in the net) in SPDispose Check.

    Martin Hatch said that
    I "should not dispose of any "SPContext.Current" objects" but the using statement is expanded to a

    finally
    {
        theSite.Dispose();
    }

    RichieH said "I also read that you should dispose of the RootWeb object whenever you use the SPSite."

    but Stefan Goßner answers his question "4) When should I dispose SPSite.RootWeb?" a little bit different. I understand it in the way that I shall never call RootWeb.Dispose(). Is this correct?


    Wednesday, April 22, 2009 8:42 AM
  • The SPSite and SPWeb objects associated with SPContext.Currrent {Site | Web} are shared in the HttpContext of the page.
    I have seen MANY MANY times where people have disposed of SPContext.Current.Web or SPContext.Current.Site .. and another webpart on the page has broken!

    So in answer to your question:

    You should not dispose the SPSite.RootWeb IF you have gotten your SPSite object from SPContext.Current.Site!

    The best practice method would be:

    using (SPSite mySite = new SPSite(SPContext.Current.Site.Url))
    {
        ...
    }


    This will create a brand new SPSite object (from the "new" constructor).

    I would also recommend that you don't use SPSite.RootWeb, instead use:

    SPWeb rootWeb = SPSite.OpenWeb(SPSite.RootWeb.ID);

    Again, this creates a brand new SPWeb object. You don't need to dispose of this manually, the SPSite "using" statement will do that for you.



    regards
    Martin Hatch
    MCPD .Net Web Development
    MCTS WSS 3.0 | MOSS 2007
    Visit my Blog
    • Marked as answer by Mike Walsh FIN Wednesday, April 22, 2009 9:24 AM
    Wednesday, April 22, 2009 9:02 AM