locked
Correctly disposing SPContext.Current.Site.RootWeb

    Question

  • Hi there,

     

    I'd like to know if RootWeb needs to be disposed when accessed via a property of SPContext? I know if I create the SPSite object myself it does, but what about when accessing SPContext.Current.Site.RootWeb?

     

    Thanks for any help! Alex.

    Friday, February 01, 2008 12:44 PM

Answers

  • If you ask me, I would say yes: do not dispose it as some other objects might require them later on in the runtime.

     

    Friday, February 01, 2008 1:37 PM

All replies

  • If I'm not wrong you shouldn't dispose any of the objects obtained through SPContext.

     

    Friday, February 01, 2008 1:09 PM
  • Yes that's my understanding as well - certainly you wouldn't dispose of SPContext.Current.Site. I was wondering if the same rule applied for RootWeb and other related properties that can be obtained from SPContext.Current.Site.

     

    Thanks, Alex.
    Friday, February 01, 2008 1:35 PM
  • If you ask me, I would say yes: do not dispose it as some other objects might require them later on in the runtime.

     

    Friday, February 01, 2008 1:37 PM
  • There seems to be quite some confusion on the matter out there!

     

    First of all, read Stefan Goßners really good blog post on the http://blogs.technet.com/stefan_gossner/archive/2008/05/07/troubleshooting-spsite-spweb-leaks-in-wss-v3-and-moss-2007.aspx

     

    Secondly, Roger Lamb has written a good blog post with best practises:

    http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx

     

    He states that SPContext.Current.Site.RootWeb SHOULD be disposed.

     

    If we look at the actual code for RootWeb:

    public SPWeb RootWeb

    {

        get

        {

            if (this.m_rootWeb == null)

            {

                this.m_rootWeb = this.OpenWeb(this.ServerRelativeUrl);

                this.m_rootWebCreated = true;

            }

            return this.m_rootWeb;

        }

    }

     

    It is seen here that OpenWeb is used, which means that the object need disposal!

     

    And a last word. It is ok to dispose it even it is used again, as m_rootWeb  is set to null on disposal.

     

    Correct me if I'm wrong! A word from MS would be nice - hey I have an idea: Why not put this stuff in MSDN docs!?! I suggest here:

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.rootweb.aspx

     

    Thursday, May 29, 2008 8:36 AM
  •  Waldek Mastykarz wrote:
    If I'm not wrong you shouldn't dispose any of the objects obtained through SPContext.

     

     

    That is correct.  In this case, however, you must dispose of the SPWeb object returned by the SPSite.RootWeb property.

     

    SPContext.Current.Site.RootWeb is just a short cut to the following:

     

    Code Snippet

    SPSite site = SPContext.Current.Site; // do not dispose of site

    SPWeb rootWeb = site.RootWeb;         // dispose of rootWeb

     

     

     

     

    Friday, May 30, 2008 8:57 PM
  • hi all, IMHO there is no need to dispose it.

    Taking a look to the SPSite.Dispose() and SPSite.OpenWeb() implementation, you can see that when SPSite is disposed, all opened webs (including RootWeb, if it's been used at least once) are disposed too.

    So, if you want, you can explicitly dispose the RootWeb object, thus anticipating the "automatic" disposal; but what if another control/object uses the RootWeb property of the same SPSite? As you can see in the previous posts, the SPSite object re-opens it.

    Two typical scenarios:

    1) somewere in my page I use of SPContext.Current.Site.RootWeb.
    If I dispose it, any other object that use the SPContext.Current.Site.RootWeb in the same SPContext, will cause the SPContext.Current.Site to re-open it. Not a problem. But not efficient.
    The page will dispose for me the SPSite as well the .RootWeb.

    2) I'm using a SPSite I've instantiated by myself. In this case, I MUST dispose it as soon as possible: therefore the RootWeb will be disposed too, without need to explicit disposal.

    using (SPSite s = new ...) { 
       // here my code uses s.RootWeb
    } // here SPSite's disposal causes
    the disposal of RootWeb

    hope this helps

     

    Thursday, February 10, 2011 8:16 AM