2

I am writing code in feature receiver, and I am wondering when to dispose SPWeb and SPSite objects.

For example, I have this in my code:

SPWeb web = properties.Feature.Parent as SPWeb;

Do I dispose the web object in this case? I know I should not do it if it's in SPContext, but what about properties.Feature?

My second case is, I have also a method that accepts SPSite as a parameter, so I pass myweb.Site to this method. Should I dispose the Site object I passed as below?

    public string doSomething(SPSite site)
        {
         //some code here
        }

third case, can anyone confirm that I must dispose web objects returned by OpenWeb function?

2

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

  • In the case of a method, should I dispose of the site object in the original method, or in the called method? If I dispose it in the called method? Would it that be considered as disposed in the caller method? – Jacky Sep 26 '16 at 23:14
  • Dear @Jacky this good question , you should dispose the object at called method first as I mentioned above then dispose of the object at original method so you can implement as the following void originalmethod () { using (SPSite site = new SPSite("moss")) { doSomething(site); } } and at doSomething(SPSite site) {//code site.dispose()} ,please tell me if you need more clarification – Mohamed El-Qassas MVP Sep 26 '16 at 23:34
  • 1
    Thanks M.Qassas, but isn't this the same object passed from one place to another, if you dispose it in the second method, won't it affect the first method? – Jacky Sep 27 '16 at 0:12
  • 1
    but it is a class, not a primitive type, so it should be passing by reference. Right? – Jacky Sep 27 '16 at 10:31
  • Dear @Jacky , I am sorry but I need to simplify answer at this comment , yes, you are right, it's not primitive type but it's also not passed by reference because it's a reference type that by default passed by value in C#. , at reference type only the reference that has been copied not the entire object so no need for dispose within called function, this what made me confused at the above comment :) so let me update my answer with accurate details to be useful for all , – Mohamed El-Qassas MVP Sep 27 '16 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.