Freigeben über


SharePoint 2007 PublishingWeb.GetVariation() Method Leak

PublishingWeb.GetVariation() method returns a PublishingWeb object which which needs to be explicitly Closed() as shown in the following code sample.  For a complete list of all the updated SharePoint Dispose() guidance please see SharePoint 2007 and WSS 3.0 Dispose Patterns by Example .

 void GetVariationLeak()
{
    using (SPSite siteCollection = new SPSite("https://moss"))
    {
        using (SPWeb web = siteCollection.OpenWeb())
        {
            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);  // Passing in web so no Close() needed
            VariationLabel variationLabel = Variations.Current.UserAccessibleLabels[0];
            PublishingWeb variationPublishingWeb = publishingWeb.GetVariation(variationLabel);  // must be Closed()
            // ...
        } // SPWeb object web.Dispose() automatically called
    }  // SPSite object siteCollection.Dispose() automatically called 
}

void GetVariationNoLeak()
{
    using (SPSite siteCollection = new SPSite("https://moss"))
    {
        using (SPWeb web = siteCollection.OpenWeb())
        {
            PublishingWeb variationPublishingWeb = null;
            try
            {
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);  // Passing in web so no Close() needed
                VariationLabel variationLabel = Variations.Current.UserAccessibleLabels[0];
                variationPublishingWeb = publishingWeb.GetVariation(variationLabel);  // must be Closed()
                // ...
            }
            finally
            {
                if(variationPublishingWeb != null)
                    variationPublishingWeb.Close();
            }
        } // SPWeb object web.Dispose() automatically called
    }  // SPSite object siteCollection.Dispose() automatically called 
}

Special thanks to Keith Dahlby, Stefan Goßner (Microsoft), and Robert Orleth (Microsoft).

Comments