Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

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)

share|improve this answer
    
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. –  Meyer Denney Aug 1 '12 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 –  Per Jakobsen Aug 1 '12 at 14:49
add comment

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?

share|improve this answer
2  
site.RootWeb does not need to be disposed of (as per SPDispose guidance) –  Louis Aug 1 '12 at 10:53
add comment

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:

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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