Close PublishingWeb created by PublishingWebCollection.Add |
PublishingWebCollection.Add() method requires you to call Close on the returned PublishingWeb object.
CheckId | SPC110263 |
---|---|
TypeName | ClosePublishingWebCreatedByPublishingWebCollectionAdd |
Severity | CriticalWarning |
Type | Assembly |
PublishingWebCollection.Add() method requires you to call Close() on the returned PublishingWeb object. See sample from MSDN:
Bad Practice
void PublishingWebAddLeak()
{
using (SPSite siteCollection = new SPSite("http://moss"))
{
using (SPWeb web = siteCollection.OpenWeb())
{
// Passing in SPWeb object that you own, no dispose needed on outerPubWeb.
PublishingWeb rootPublishingWeb = PublishingWeb.GetPublishingWeb(web);
PublishingWebCollection publishingWebs = rootPublishingWeb.GetPublishingWebs();
// Create the new PublishingWeb using the example values provided.
PublishingWeb newWeb = publishingWebs.Add("http://moss/site", 1033, "STS#0"); // PublishingWeb will leak
} // SPWeb object web.Dispose() automatically called.
} // SPSite object siteCollection.Dispose() automatically called.
}
{
using (SPSite siteCollection = new SPSite("http://moss"))
{
using (SPWeb web = siteCollection.OpenWeb())
{
// Passing in SPWeb object that you own, no dispose needed on outerPubWeb.
PublishingWeb rootPublishingWeb = PublishingWeb.GetPublishingWeb(web);
PublishingWebCollection publishingWebs = rootPublishingWeb.GetPublishingWebs();
// Create the new PublishingWeb using the example values provided.
PublishingWeb newWeb = publishingWebs.Add("http://moss/site", 1033, "STS#0"); // PublishingWeb will leak
} // SPWeb object web.Dispose() automatically called.
} // SPSite object siteCollection.Dispose() automatically called.
}
Good Practice
void PublishingWebAddNoLeak()
{
using (SPSite siteCollection = new SPSite("http://moss"))
{
using (SPWeb web = siteCollection.OpenWeb())
{
PublishingWeb newWeb = null;
// Passing in SPWeb object that you own, no dispose needed on outerPubWeb.
PublishingWeb rootPublishingWeb = PublishingWeb.GetPublishingWeb(web);
PublishingWebCollection publishingWebs = rootPublishingWeb.GetPublishingWebs();
try
{
newWeb = publishingWebs.Add("http://moss/site", 1033, "STS#0"); // PublishingWeb will leak
}
finally
{
// Always close the SPWeb when done to release memory.
if (null != newWeb)
{
newWeb.Close();
}
}
} // SPWeb object web.Dispose() automatically called.
} // SPSite object siteCollection.Dispose() automatically called.
}
{
using (SPSite siteCollection = new SPSite("http://moss"))
{
using (SPWeb web = siteCollection.OpenWeb())
{
PublishingWeb newWeb = null;
// Passing in SPWeb object that you own, no dispose needed on outerPubWeb.
PublishingWeb rootPublishingWeb = PublishingWeb.GetPublishingWeb(web);
PublishingWebCollection publishingWebs = rootPublishingWeb.GetPublishingWebs();
try
{
newWeb = publishingWebs.Add("http://moss/site", 1033, "STS#0"); // PublishingWeb will leak
}
finally
{
// Always close the SPWeb when done to release memory.
if (null != newWeb)
{
newWeb.Close();
}
}
} // SPWeb object web.Dispose() automatically called.
} // SPSite object siteCollection.Dispose() automatically called.
}
Rule relates to SPDisposeCheckId 'SPDisposeCheckID_310'. To ignore this rule add the attribute '[SPDisposeCheckIgnore(SPDisposeCheckID.SPDisposeCheckID_310, "Caller will dispose")]' to your method.
To suppress this violation in managed code add the following attribute to the method which contains the instruction (available since SPCAF version v5.2). Learn more about SuppressMessage here.
// Important: Ensure to have #define CODE_ANALYSIS at the beginning of your .cs file
[SuppressMessage("SPCAF.Rules.MemoryDisposalGroup", "SPC110263:ClosePublishingWebCreatedByPublishingWebCollectionAdd", Justification = "Provide reason for suppression here")]
[SuppressMessage("SPCAF.Rules.MemoryDisposalGroup", "SPC110263:ClosePublishingWebCreatedByPublishingWebCollectionAdd", Justification = "Provide reason for suppression here")]
Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of Rencore. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.