Solved

Does SPWeb need to be disposed in this code sample?

Posted on 2010-08-17
5
1,515 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
  • 2
  • 2
5 Comments
 
LVL 31

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 31

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
A question that is asked often, is how to generate sequential numbers in InfoPath Forms. The best way to achieve this is to use a SQL database, along with a stored procedure and a web service to connect Forms Services to the DB. The first thing t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

792 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