SPSite, SPWeb Dispose and Class Design Partter

Need to Dispose:
 
1. SPSite oSPsite = new SPSite(http://server)
2. siteCollection = new SPSite(SPContext.Current.Web.Url))
3. SPSite siteCollection = siteCollections.Add("sites/myNewSiteCollection", "DOMAIN\\User",
      roger.lamb@litwareinc.com)
 
4.   using (SPWeb web = new SPSite(SPContext.Current.Web.Url).OpenWeb())
    {
        // SPSite leaked !
    }
 
5.     UserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(siteCollection));
        UserProfile profile = profileManager.GetUserProfile("domain\\username");
        SPSite personalSite = profile.PersonalSite;    // Will leak.

 
6. SPWeb oSPWeb = oSPSite.OpenWeb()
7. SPWeb web = siteCollection.AllWebs.Add("site-relative URL");
8.  using (SPWeb web = siteCollection.OpenWeb())
 {
        SPWeb addedWeb = web.Webs.Add(strWebUrl);   // Will leak.

 }
 
9.  for(i = 0;i < oSPWeb.Webs.Count;i++)
  {
      oSPWeb2 = oSPWeb.Webs[i];
      BuildTableRow(oDisplayTable, "Web", oSPWeb2.Title);
      oSPWeb2.Dispose();
  }
10.   Area area = AreaManager.GetArea(PortalContext.Current, new Guid("{GUID}"));
    using (SPWeb areaWeb = area.Web)
    {
        string str = areaWeb.Title;
    }
 
11.  using (SPSite siteCollection = new SPSite("http://moss"))
      {
        using (SPWeb web = siteCollection.OpenWeb())
        {
            SPFile page = web.GetFile("Source_Folder_Name/Source_Page");
            SPLimitedWebPartManager webPartManager =
                page.GetLimitedWebPartManager(PersonalizationScope.Shared);
                webPartManaber.Web.Dispose();
        } // SPWeb object web.Dispose() automatically called.
      }  // SPSite object siteCollection.Dispose() automatically called.

12.  using (SPWeb web = siteCollection.OpenWeb())
        {
            PublishingWeb variationPublishingWeb =
null;
           
try
            {
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web); 
// Passing in SPWeb object, so no Close() needed.
                VariationLabel variationLabel = Variations.Current.UserAccessibleLabels[0];
                variationPublishingWeb = publishingWeb.GetVariation(variationLabel); 
// Must be Closed().
                // ...
            }
           
finally
            {
               
if(variationPublishingWeb != null)
                    variationPublishingWeb.Close();
            }
        }
//

 
Don't Need to Dispose:
 
1. SPWeb web = SPControl.GetContextWeb(HttpContext.Current)
2. SPWebCollection webCollection = siteCollection.AllWebs;
 
3. using (SPSite siteCollection = new SPSite("http://moss"))
    {
        using (SPWeb outerWeb = siteCollection.OpenWeb())
        {
            foreach (SPWeb innerWeb in siteCollection.AllWebs)
            {
                try
                {
                    // ...
                }
                finally
                {
                    if(innerWeb != null)
                        innerWeb.Dispose();
                }
            }
        } // SPWeb object outerWeb.Dispose() automatically called.
    }
 
4.  IPersonalPage currentMySitePage = this.Page as IPersonalPage;
if (currentMySitePage != null && !currentMySitePage.IsProfileError)
{
     SPSite personalSite = currentMySitePage.PersonalSite; // Will not leak.
     // ...
}
 
5. SPWeb parentWeb = list.ParentWeb; //No explicit dispose required.

6. SPSite siteCollection = SPControl.GetContextSite(Context);
7. SPWeb web = SPControl.GetContextWeb(Context);
8. PublishingWeb outerPubWeb = PublishingWeb.GetPublishingWeb(web);
    PublishingWebCollection pubWebCollection = outerPubWeb.GetPublishingWebs();
 
For and Foreach
             foreach (siteCollectionInner in siteCollections)
             {
                      try  //Should be first statement after foreach.
                      {
                          Console.WriteLine(siteCollectionInner.Url);
                          //Exception occurs here.
                      }
                      finally
                      {
                          if(siteCollectionInner != null)
                          siteCollectionInner.Dispose();
                 }
 
 
Class Design Pattern
public MyClass
{
     private SPSite _site;
     private SPWeb _web;

     MyClass()
    {
      _site = new SPSite(url);
       _web = _site.OpenWeb();
     }
    public void MethodA()
    {
        _siteCollection = new SPSite("http://moss");
        _web = _siteCollection.OpenWeb();
    }

    public void MethodB()
    {
        if (_web != null)
        {
            string title = _web.Title;
        }
    }

    public void MethodC()
    {
        if (_web != null)
        {
            string name = _web.Name;
        }
    }


//do stuff w/member variables

//dispose of object to prevent memory leaks
    ~MyClass()
  {
   if ( _site == null )
    {
       _site.Dispose();
      _site = null;
    }

     if ( _web == null )
    {
       _web.Dispose();
      _web = null;
     }
   }
}
 
原文地址:https://www.cnblogs.com/gzh4455/p/2233299.html