locked
Disposing object in sharepoint RRS feed

  • Question

  • Hi All,

    I'm writing some code that uses SPSite and SPWeb objects. I read the following article

    http://msdn.microsoft.com/en-us/library/aa973248.aspx

    And I'm trying to follow the rules there to avoid memory leaks. However, one thing that is not clear from the article is whether the order of disposal metters. That is, if I call:

        SPSite site = new SPSite(...);

    and then:

        SPWeb web = site.AllWebs[...];

    is it important that I dispose web first and than site? can I dispose site and still use web?

    thanks,
    ok

    Sunday, November 9, 2008 5:42 AM

Answers

  • Hi,

    In SharePoint the hierarchy is like this,

    SPFarm - SPWebApplication - SPSite - SPWeb.

    You need to dispose the web object first and then dispose site object. 

    If you disposed site object, after that you cant use web object.
    Gopinath D - http://sharepointsnippets.com
    Sunday, November 9, 2008 6:53 AM

All replies

  • Hi,

    In SharePoint the hierarchy is like this,

    SPFarm - SPWebApplication - SPSite - SPWeb.

    You need to dispose the web object first and then dispose site object. 

    If you disposed site object, after that you cant use web object.
    Gopinath D - http://sharepointsnippets.com
    Sunday, November 9, 2008 6:53 AM
  • thanks!
    Sunday, November 9, 2008 6:58 AM