Just to clarify on what you said with that statment,
An earlier version of this article recommended that the calling
application should dispose of the SPWeb.ParentWeb. This is no longer
the official guidance. The dispose cleanup is handled automatically by
the SharePoint framework.
to my understanding:
from where you start the spweb object within the using statment that needs disposing it will dispose anything within it that is being called!
using (SPWeb web = site.OpenWeb())
{
//call parent web within
}
the using statment handles the idisposable of any object tree within.
just to clarify what im talking about!
An earlier version of this article recommended that the calling
application should dispose of the SPWeb.ParentWeb. This is no longer
the official guidance. The dispose cleanup is handled automatically by
the SharePoint framework.
Good Disposing
using (SPSite site = new SPSite(http://localhost))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Announcements"];
SPWeb parentWeb = list.ParentWeb; //No explicit dispose required.
}
}
I think what they are refering to is sharepoint 2003 as it runs under older .net and older sharepoint.dll ;) but for 2007+ using the using statment that calls the idisposable of any newly created object within is the way to go!
obviously using the using statment is still fine grained, by that i mean disposing of recursive spweb within webs even within the using statment of the spweb will cause leaks:
Bad disposing
void WebsLeak()
{
using (SPSite siteCollection = new SPSite("http://moss"))
{
using (SPWeb outerWeb = siteCollection.OpenWeb())
{
foreach (SPWeb innerWeb in outerWeb.Webs)
{
// SPWeb innerWeb leak.
}
} // SPWeb object outerWeb.Dispose() automatically called.
} // SPSite object siteCollection.Dispose() automatically called.
}
Good Disposing
void WebsNoLeak()
{
using (SPSite siteCollection = new SPSite("http://moss"))
{
using (SPWeb outerWeb = siteCollection.OpenWeb())
{
foreach (SPWeb innerWeb in outerWeb.Webs)
{
try //Should be first statement after foreach.
{
// ...
}
finally
{
if(innerWeb != null)
innerWeb.Dispose();
}
}
} // SPWeb object outerWeb.Dispose() automatically called.
} // SPSite object siteCollection.Dispose() automatically called.
}
more on disposing found here:
http://msdn.microsoft.com/en-us/library/aa973248.aspx
and on list parnet web objects
http://solutionizing.net/2009/01/09/splist-parentweb-leaks-revisited/
as for the spcontext robert is correct, but not fully ;) there are some methods that you have to dispose of or use the using statment to dispose of it!
I have gone into detail on:
SPContext.Current.Site.OpenWeb()
That above is the exception to disposing of the object as it creates a new object from the current context ;)
What is the difference between SPContext.Current.Web and SPContext.Current.Site.OpenWeb()?
EDIT
to clarify on your example:
SPContext.Current.Web.ParentWeb.ParentWeb
again the above handles the idisposable and you dont need to explicitly call the dispose method! like i said above it needs to be within using statment of a web object or site object! if you explicitly dispose of the object your getting rid of the current parent parent object which will throw exceptions!
Good
using (SPWeb web = SPContext.Current.Site.OpenWeb())
{
SPWeb parentWeb = SPContext.Current.Web.ParentWeb.ParentWeb; //No explicit dispose required
}
Bad
SPWeb parentWeb = SPContext.Current.Web.ParentWeb.ParentWeb;
the above is showing your creating a new spweb object that needs disposing because your using the current context openweb which creates a new web object but using the current context. within the using you can then call the recursive call to parent web as its handled by sharepoint regardless of the recursive calls made by ParentWeb as its already handled within the using statment! if you did call dispose than to what even parentweb you have disposed it will not only dispose of that object but also its children webs!
more on what im talking about and a solution to resources on openwebs that rely on currentcontext can be found here:
http://solutionizing.net/2009/03/19/introducing-spweb-getparentweb/
SPSite
will call Dispose on allSPWeb
objects created from it. As long as you never dispose a Context site, all will be fine. A lot of this is blown out of proportion these days, as SharePoint will happily reuse a Disposed SPSite or SPWeb object anyway. – James Love Dec 6 '13 at 9:36