Solved

Does SPWeb need to be disposed in this code sample?

Posted on 2010-08-17
5
1,534 Views
Last Modified: 2012-05-10
I'm using the current context to get the SPWebApplication the user is in.  I then use the SPWebApplication to get a specific SPWeb. I then use that SPWeb to get a SPWebCollection.

Because I am indirectly using the current context to get SPWeb and SPWebCollection I am not sure what if anything needs to be disposed.
SPWebApplication spwa = SPControl.GetContextWebApplication(HttpContext.Current);

SPWeb rootWeb = spwa.Sites["Programs"].RootWeb;
SPWebCollection subWebs = rootWeb.Webs;

foreach (SPWeb oWeb in subWebs)
{
   if (oWeb.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open))
   {
      programSiteList.Add(oWeb);
   }
}

Open in new window

0
Comment
Question by:dmoss123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
ID: 33457033
Your oWeb instances probably need to be disposed to be on the safe side. It shouldn't effect the SPContext as you also state.

Consider using Dispose Checker:

http://spdisposecheckstatic.codeplex.com/
0
 

Author Comment

by:dmoss123
ID: 33458851
Would this be a good way of ensuring oWeb is disposed of properly?

foreach (SPWeb oWeb in subWebs)
{
   try
   {
      if (oWeb.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open))
      {
         programSiteList.Add(oWeb);
      }
   }
   finally
   {
      if (oWeb != null)
      {
         oWeb.Dispose();
      }
   }
}
0
 

Author Comment

by:dmoss123
ID: 33458896
I actually have Dispose Checker installed as part of the SPSF Factory http://spsf.codeplex.com/ .When I run it reports 15 errors similar to the following. Do I need to be concerned about this?

Call to Microsoft.SharePoint.Administration.SPSiteCollection.get_Item and no variable to catch return value rootWeb := spwa.{Microsoft.SharePoint.Administration.SPWebApplication}get_Sites().{Microsoft.SharePoint.Administration.SPSiteCollection}get_Item("Programs").{Microsoft.SharePoint.SPSite}get_RootWeb(), see:http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_230
0
 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 33462832
Your code sample would do the trick. To be honest your oWeb instances are probably going to be alright from a foreach loop .

The messages from SPDisposeCheck mean what they say, you are calling methods that are returning objects that don't get assigned to a variable, which risks the items not being disposed. Worth checking the lines it refers you to.
0
 
LVL 12

Expert Comment

by:Hairbrush
ID: 33462951
I think you also need to dispose of rootWeb.  Try using a "using" construct to achieve that, which is tidier than the try/finally.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For SharePoint sites, particularly public-facing ones, there are times when adding JavaScript, Meta Tags, CSS Styles or other content to the page <head> section is more practical than modifying master pages.  For instance, you could add the jQuery l…
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question