Do I dispose of the web object in this case?
No. Do not dispose SPFeatureReceiverProperties.Feature.Parent
, For more details check SPFeatureReceiverProperties.Feature.Parent
Should I dispose of the Site object I passed as below?
In this case,Site
is a reference variable type that by default passed by value in C# , So for reference type only the reference for the same object that has been copied not the entire object .
So you don't need to dispose it at the called function doSomething
, it's sufficient to dispose it at the originalmethod
method , so your code should look like
Public void originalmethod () {
using (SPSite site = new SPSite("moss"))
{
doSomething(site);
}
}
public string doSomething(SPSite site) {//don't dispose site object here}
Can anyone confirm that I must dispose of web objects returned by OpenWeb function?
Consider
SPSite siteCollection = new SPSite("http://moss");
SPWeb web = siteCollection.OpenWeb();
So OpenWeb()
will construct a new SPWeb
object which you actually have to dispose of as
using (SPSite siteCollection = new SPSite("http://moss"))
{
using (SPWeb web = siteCollection.OpenWeb())
{
} // SPWeb object web.Dispose() automatically called.
} // SPSite object siteCollection.Dispose() automatically called.
Also at event receiver, it should be disposed as
SPWeb web = properties.OpenWeb()
web.dispose();
For more details check Disposing SPWeb and SPSite objects