Solved

Does SPWeb need to be disposed in this code sample?

Posted on 2010-08-17
5
1,524 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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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. …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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