Solved

Does SPWeb need to be disposed in this code sample?

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I recently came across an issue with a MOSS 2007 deployment where access into some sub-sites were denied, even for the MOSS farm administrators. A bit of background to the setup of this MOSS farm; this was a three server setup, consisting of a fr…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

16 Experts available now in Live!

Get 1:1 Help Now