Solved

Does SPWeb need to be disposed in this code sample?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you create your solutions on SharePoint sooner or later you will come upon a request to set  permissions of the item depending on some of the item's meta-data - the author, people assigned as approvers, divisions, categories etc. The most natu…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now