2

In some cases such as:

using (SPSite site = new SPSite(@"http://sitecollection/"))
{
    SPWeb web = site.RootWeb;
}

you don't need to dispose of the SPWeb object because it is disposed of automatically by the garbage collector. Same with SPItemEventProperties, e.g.

    private static void ItemUpdated(SPItemEventProperties properties)
    {
        SPWeb web = properties.Web;     
    }

What are the dangers of disposing of these SPWeb objects when they are already automatically disposed of? It has been so ingrained to always dispose of SPWeb objects that in some of my older code, I have disposed of the SPWeb object returned from site.RootWeb or properties.web

4 Answers 4

2

In the first example there is no problem in disposing the SPWeb, because you're sure nobody is going to use it later.

In the second example you run the risk of a memory leak or a crash if there is any event receiver with a higher sequence on the same list/content type. The other event receiver will probably try to use the Web which has been disposed, SharePoint will the try to revitalize the Web, but may fail/have to create a new SPWeb depending on garbage collection status.

So if you haven't created the SPSite/SPWeb (SPContext.Current, ..EventProperties, ...) then you shouldn't deallocate it (unless you have to as with SPLimitedWebPartManager.Web)

2
  • Thanks for the info! Do you think you could clarify that last sentence a little? :) Are you saying if I haven't created SPSite/SPWeb from SPContext/EventProperties then I shouldn't dispose? it? I thought those were the the only times I could safely not worry about disposing of it. Aug 1, 2012 at 14:31
  • 1
    Any SPSite/SPWeb you get from SPContext/EventProperties may be passed to other WebParts/EventReceivers, so you're not allowed to Dispose these Aug 1, 2012 at 14:49
2

In general, you should only dispose an object that you are created.

using (SPSite site = new SPSite(@"http://sitecollection/"))       
{           
    SPWeb web = site.RootWeb;       
}

In example 1, the instantiator is you. Doesnt matter if you call dispose in the middle of your code (as long as the object is not needed anymore) or wait until it dispose automatically at the end of using branchlet.

private static void ItemUpdated(SPItemEventProperties properties)       
{
    SPWeb web = properties.Web; 
}   

In example 2 you just reference it to an existing object, so its not recommended to dispose it because maybe the instantiator (your event receiver) still need it. What happen if you destroyed an object that you dont belong?

1
  • 2
    site.RootWeb does not need to be disposed of (as per SPDispose guidance)
    – Louis
    Aug 1, 2012 at 10:53
2

The only real 'danger' is that you will cause the API to re-fetch the metadata for that object. Depending on your code, your context and the SPWeb/SPSite objects, this can potentially be a large performance hit and therefore a significant flaw.

You will not cause exceptions or hard errors by disposing too early or too late, it's only messing up SharePoint internal caching mechanisms.

Some references:

1

I would recommend installing and using SPDisposeCheck, if you haven't already; I know that I'm gradually running it on older and older bits of code and having to fix issues.

The rules of thumb mentioned above are true - if you didn't instantiate it, you shouldn't dispose of it.

Note that with SPDisposeCheck the .RootWeb property is problematic - it warns you if you don't dispose of the web, and it warns you if you do. This is a bug. The truth is you don't need to dispose of it, the SPSite disposal will get rid of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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