2
votes

If you start SharePoint and learn about disposing SPSite and SPWeb, almost everybody suggests to use the "using"-construct if you work with new instances of SPSite and SPWeb.

using (SPSite s = new SPSite("http://yourserver")
{
    using (SPWeb web = s.OpenWeb("http://yourweb")
    {
         // do fancy stuff here
    }
}

I did that too. However, if you look into the SPSite.Dispose() method, you'll notice that SPSite keeps a reference of its SPWebs created by SPSite.OpenWeb(). On SPSite.Dispose(), all SPWebs tied to this SPSite are diposed. I think (but Im not sure) that virtually all methods which are creating a new SPWeb are adding this SPWeb to the internal collection 'm_openedWebs'.

This being said, I wonder why you would ever dispose a SPWeb object? Are there any real-world scenarios were you should dispose SPWeb objects?

UPDATE

I just realized, that I spared a little detail which might be important. The SPSite object does not call Dispose() on its child SPWebs, but it is calling Close(). In the current implementation of SharePoint, this is the same as Dispose(), becasue SPWeb.Dispose() just contains:

public void Dispose()
{
    this.Close();
}

So one reason for disposing SPWeb objects might be to be compatible to future SharePoint implementations where SPWeb.Dispose() does not only call SPWeb.Close(). I still find it akward to "double-dispose" my SPWebs only for that reason.

4
before I correct it, anyone agree or disagree that in the last paragraph, the second SPWeb.Dispose() should actually be SPWeb.Close()? I'll be back some day to correct if I don't receive criticism that changes my mindCode Jockey
@CodeJockey: You're right! I fixed it.binford

4 Answers

2
votes

You're right, disposing the SPSite will close the associated SPWeb objects.

The main scenario where you want to close SPWeb's right away is when you do something like:

foreach (SPWeb web in site.AllWebs) 
{ 
     // do something with the web object 
    web.dispose();        // <-- this is mandatory!
}

For normal opening and closing of one SPSite/SPWeb this doesn't matter but I would argue it is still good practice (especially for new SharePoint developers).

If you haven't already, I would highly recommend reading Stefan Goßner's post: http://blogs.technet.com/b/stefan_gossner/archive/2008/12/05/disposing-spweb-and-spsite-objects.aspx

0
votes

That's an interesting dilemma, binford and I certainly wasn't aware until now that the SPSite object was implicity disposing of any SPWebs tied to it.

If what you're saying is so, then the only reason I could think you might still want to explicitly handle the SPWeb might be when the scenario is more complex, for example using two or more SPWebs where the time to construct and destruct them thus becomes a more important consideration.

For example, imagine you had 7 SPWeb instantiations inside your SPSite using block, each performing a series of long-running and complex operations before disposing -- you'd want to be closing and disposing of these objects as soon as possible to maximise memory resources and garbage collect efficiently.

That said, in the most basic of cases (1 SPSite, 1 SPWeb as you have detailed above), I am beginning to wonder if the SPWeb explicit dispose is at all necessary.

More research and opinions needed I think, but thank you for alerting my attention to this.

0
votes

If you're reading/writing data to/from multiple SPWebs instantiated within an SPSite, and you lose control over which you have open, you will soon encounter data concurrency issues, where you're having to do AllowUnSafeUpdates all over the place which is generally bad practice.

Chris O'Brien has a good blog post on SPWeb and IDisposable: http://www.sharepointnutsandbolts.com/2008/06/disposing-sharepoint-objects-what-they.html

0
votes

I would make sure you are using SPDisposeCheck. After Visual Studio, this is the must have tool for SharePoint development. The latest version now includes rules on when not to dispose objects:

We have added several checks on when “NOT” to Dispose objects instantiated by SharePoint internally. These newly reported “DO NO DISPOSE” (DND) rules were unreported by SPDisposeCheck v1.3.* .