<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Solutionizing .NET &#187; ParentWeb</title>
	<atom:link href="http://solutionizing.net/tag/parentweb/feed/" rel="self" type="application/rss+xml" />
	<link>http://solutionizing.net</link>
	<description>Random thoughts on custom development in SharePoint.</description>
	<lastBuildDate>Fri, 03 Feb 2012 05:41:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='solutionizing.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Solutionizing .NET &#187; ParentWeb</title>
		<link>http://solutionizing.net</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://solutionizing.net/osd.xml" title="Solutionizing .NET" />
	<atom:link rel='hub' href='http://solutionizing.net/?pushpress=hub'/>
		<item>
		<title>Introducing SPWeb.GetParentWeb()</title>
		<link>http://solutionizing.net/2009/03/19/introducing-spweb-getparentweb/</link>
		<comments>http://solutionizing.net/2009/03/19/introducing-spweb-getparentweb/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 17:56:47 +0000</pubDate>
		<dc:creator>Keith Dahlby</dc:creator>
				<category><![CDATA[Object Model]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[dispose]]></category>
		<category><![CDATA[ParentWeb]]></category>
		<category><![CDATA[SPDisposeCheckIgnore]]></category>
		<category><![CDATA[SPWeb]]></category>

		<guid isPermaLink="false">http://solutionizing.net/?p=409</guid>
		<description><![CDATA[Official Microsoft guidance is to never explicitly Dispose() SPWeb.ParentWeb. I generally agree with this advice, given that my Rule #1 of SharePoint disposal is that &#8220;Using a disposed object can cause more problems than failing to dispose.&#8221; To understand why, I&#8217;ll borrow my explanation from SPDevWiki: This property will allocate an SPWeb object the first [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solutionizing.net&amp;blog=3982353&amp;post=409&amp;subd=solutionizing&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a title="SharePoint 2007 and WSS 3.0 Dispose Patterns by Example" href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_170">Official Microsoft guidance</a> is to never explicitly <code>Dispose()</code> <code>SPWeb.ParentWeb</code>. I generally agree with this advice, given that my Rule #1 of SharePoint disposal is that &#8220;Using a disposed object can cause more problems than failing to dispose.&#8221; To understand why, I&#8217;ll borrow my <a title="When to Dispose SharePoint objects" href="http://sharepointdevwiki.com/display/public/When+to+Dispose+SharePoint+objects#WhentoDisposeSharePointobjects-SPWeb.ParentWeb">explanation from SPDevWiki</a>:</p>
<blockquote><p>This property will allocate an <code>SPWeb</code> object the first time it is called. The caveat is that once it is disposed, any reference to the property will return the disposed object. If an <code>SPWeb</code> is not owned by the developer, its <code>ParentWeb</code> should be considered not owned as well. For example, there could be a problem if two components both depend on <code>SPContext.Current.Web.ParentWeb</code> and one calls <code>Dispose()</code> before the other is done with it.</p></blockquote>
<p>However, this can result in memory pressure in cases involving enumeration or where the parent <code>SPSite</code> has a long lifetime. For example:</p>
<pre>SPSite contextSite = SPContext.Current.Site;
foreach(SPWeb web in contextSite.AllWebs.AsSafeEnumerable())
{
    SPWeb webParent = web.ParentWeb; // Internal OpenWeb()
    // Do something with web and webParent
}</pre>
<p>The <code>web</code> references are disposed by my safe iterator, but every <code>webParent</code> will remain open until the context <code>SPSite</code> is disposed. Not that I would recommend using code like this (in fact I would strongly urge against it), but you can never say never.</p>
<p>To that end, I propose a simple extension method whose contract is clear: always dispose me! We can still follow MS guidance regarding <code>SPWeb.ParentWeb</code>, but have convenient access to a developer-owned parent <code>SPWeb</code> as well:</p>
<pre>[SPDisposeCheckIgnore(SPDisposeCheckID.SPDisposeCheckID_120, "By Design")]
public static SPWeb GetParentWeb(this SPWeb web)
{
    if(web == null)
        throw new ArgumentNullException("web");
    return web.Site.OpenWeb(web.ParentWebId);
}</pre>
<p>And our &#8220;Best Practice&#8221; memory pressure can be revised slightly to achieve much better memory use:</p>
<pre>SPSite contextSite = SPContext.Current.Site;
foreach(SPWeb web in contextSite.AllWebs.AsSafeEnumerable())
{
    using(SPWeb webParent = web.GetParentWeb())
    {
        // Do something with web and webParent
    }
}</pre>
<p>Trivial? Obvious? Perhaps. But often the most useful code is.</p>
<p><strong>Update:</strong> Included appropriate <code>SPDisposeCheckIgnore</code> attribute for &#8220;leaked&#8221; <code>SPWeb</code> from <code>OpenWeb()</code>; we know what we&#8217;re doing. That said, you could certainly implement higher-order functions to invoke an <code>action</code> or <code>selector</code> against our imitation ParentWeb without returning it—I&#8217;ll leave those as an exercise for the reader.</p>
<br /> Tagged: dispose, ParentWeb, SPDisposeCheckIgnore, SPWeb <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solutionizing.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solutionizing.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solutionizing.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solutionizing.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solutionizing.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solutionizing.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solutionizing.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solutionizing.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solutionizing.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solutionizing.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solutionizing.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solutionizing.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solutionizing.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solutionizing.wordpress.com/409/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solutionizing.net&amp;blog=3982353&amp;post=409&amp;subd=solutionizing&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solutionizing.net/2009/03/19/introducing-spweb-getparentweb/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/26483382e3717e58e4c45d06c8ec351d?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">dahlbyk</media:title>
		</media:content>
	</item>
		<item>
		<title>SPList.ParentWeb Leaks Revisited</title>
		<link>http://solutionizing.net/2009/01/09/splist-parentweb-leaks-revisited/</link>
		<comments>http://solutionizing.net/2009/01/09/splist-parentweb-leaks-revisited/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 23:45:04 +0000</pubDate>
		<dc:creator>Keith Dahlby</dc:creator>
				<category><![CDATA[Object Model]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[BreakRoleInheritance]]></category>
		<category><![CDATA[ISecurableObject]]></category>
		<category><![CDATA[ParentWeb]]></category>
		<category><![CDATA[SPList]]></category>
		<category><![CDATA[SPSecurableObjectImpl]]></category>

		<guid isPermaLink="false">http://solutionizing.net/?p=321</guid>
		<description><![CDATA[A while back I looked into whether or not SPList.ParentWeb really needs to be disposed: Is SPList.ParentWeb a Leak? The specific motivation was to investigate why SPList.BreakRoleInheritance() requires Dispose() on ParentWeb property, as seen in Roger&#8217;s Dispose Patterns by Example. I stand by my original conclusion that this advice generally does more harm than good, but [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solutionizing.net&amp;blog=3982353&amp;post=321&amp;subd=solutionizing&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A while back I looked into whether or not <code>SPList.ParentWeb</code> really needs to be disposed: <a title="Is SPList.ParentWeb a Leak?" href="http://solutionizing.net/2008/08/10/is-splistparentweb-a-leak/">Is <code>SPList.ParentWeb</code> a Leak?</a> The specific motivation was to investigate why <a title="SPList.BreakRoleInheritance method requires Dispose() on ParentWeb property" href="http://blogs.msdn.com/rogerla/archive/2008/04/11/splist-breakroleinheritance-method-requires-dispose-on-parentweb-property.aspx"><code>SPList.BreakRoleInheritance()</code> requires <code>Dispose()</code> on <code>ParentWeb</code> property</a>, as seen in Roger&#8217;s <a title="SharePoint 2007 and WSS 3.0 Dispose Patterns by Example" href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx">Dispose Patterns by Example</a>. I stand by my original conclusion that this advice generally does more harm than good, but thought it would be useful to discuss in a bit more detail.</p>
<h2>Why dispose ParentWeb?</h2>
<p>As I showed before, the code for <code>SPList.ParentWeb</code> works like this:</p>
<pre>if (SPUtility.StsCompareStrings(this.m_Lists.Web.ServerRelativeUrl, this.ParentWebUrl))
  return this.m_Lists.Web;

if (this.m_parentWeb == null)
  this.m_parentWeb = this.m_Lists.Web.Site.OpenWeb(this.ParentWebUrl);
return this.m_parentWeb;</pre>
<p>In the vast majority of cases, the URLs will match and return the parent collection&#8217;s <code>Web</code>, which should <em>not</em> be disposed (unless, of course, you own it and know it is ready for disposal). Only in the exceptional case that the list&#8217;s <code>ParentWebUrl</code> indicates it doesn&#8217;t live with its parent collection will a new <code>SPWeb</code> be created. I believe it is this exception, rather than the norm, that leads to Roger&#8217;s suggestion that <code>ParentWeb</code> should be disposed.</p>
<p>On a curious and somewhat related note, <code>SPListItem.Web</code> doesn&#8217;t use <code>ParentWeb</code>:</p>
<pre>public SPWeb get_Web()
{
    return this.m_Items.List.Lists.Web;
}</pre>
<h2>Why BreakRoleInheritance()?</h2>
<p>Though <code>BreakRoleInheritance()</code> has been singled out, the real culprit is an internal property:</p>
<pre>private SPSecurableObjectImpl get_SecurableObjectImpl()
{
    if (this.m_SecurableObjectImpl == null)
    {
        Guid guidScopeId = (Guid) this.m_arrListProps[0x22, this.m_iRow];
        bool hasUniquePerm = guidScopeId != <strong>this.ParentWeb</strong>.RoleAssignments.Id;
        this.m_SecurableObjectImpl = new SPSecurableObjectImpl(this.ParentWeb, this, ...);
    }
    return this.m_SecurableObjectImpl;
}</pre>
<p>This property is then used in the implementations of several methods/properties from <a title="ISecurableObject Interface (Microsoft.SharePoint)" href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.isecurableobject.aspx"><code>ISecurableObject</code></a>:</p>
<ul>
<li>SPList.AllRolesForCurrentUser</li>
<li>SPList.BreakRoleInheritance()</li>
<li>SPList.HasUniqueRoleAssignments</li>
<li>SPList.ResetRoleInheritance()</li>
<li>SPList.ReusableAcl</li>
<li>SPList.RoleAssignments</li>
</ul>
<p>(Incidentally, <code>SPSecurableObjectImpl</code> is also used by <code>SPWeb</code> and <code>SPListItem</code> to implement that interface.)</p>
<p><a title="SPList.get_SecurableObjectImpl" href="http://solutionizing.files.wordpress.com/2009/01/splistget_securableobjectimpl.png"><img class="alignright size-medium" title="SPList.get_SecurableObjectImpl" src="http://solutionizing.files.wordpress.com/2009/01/splistget_securableobjectimpl.png?w=187" alt="SPList.get_SecurableObjectImpl" /></a>So now we&#8217;re dealing with several more points of entry. For example, in <code>SPListItem</code>:</p>
<pre>public ISecurableObject get_FirstUniqueAncestor()
{
    this.InitSecurity();
    if (this.RoleAssignments.Id == <strong>this.ParentList.RoleAssignments</strong>.Id)
    {
        return this.ParentList.FirstUniqueAncestor;
    }
    if (!this.HasUniqueRoleAssignments)
    {
        return this.Web.GetFolder(this.m_strPermUrl).Item;
    }
    return this;
}</pre>
<p>I could show several more obscure code paths with the same result, but the specifics are irrelevant. My point is that trying to figure out if <code>SPList.ParentWeb</code> has been referenced is a wasted effort.</p>
<h2>What to do?</h2>
<p>The way I see it, we have two options:</p>
<ol>
<li>Assume that <code>ParentWeb</code> is safe enough and if we have memory problems later we can investigate.</li>
<li>Assume that <code>ParentWeb</code> is leaky and figure out a safe way to <code>Dispose()</code> it.</li>
</ol>
<p>I&#8217;m satisfied with the former, but if you&#8217;re not — or if you have a circumstance where the <code>ParentWebUrl</code> comparison actually fails (please share) — then your code should check if it needs to dispose first:</p>
<pre>SPWeb web = SPContext.Current.Web;
SPList list;

try
{
  list = web.Lists["ListName"];
  list.BreakRoleInheritance(true);
}
finally
{
  if(list.ParentWeb != web)
    list.ParentWeb.Dispose();
}</pre>
<p>Or we can use an extension method:</p>
<pre>public static void DisposeParentWeb(this SPList list)
{
  if(list.ParentWeb != list.Lists.Web)
    list.ParentWeb.Dispose();
}</pre>
<br /> Tagged: BreakRoleInheritance, ISecurableObject, ParentWeb, SPList, SPSecurableObjectImpl <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solutionizing.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solutionizing.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solutionizing.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solutionizing.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solutionizing.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solutionizing.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solutionizing.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solutionizing.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solutionizing.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solutionizing.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solutionizing.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solutionizing.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solutionizing.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solutionizing.wordpress.com/321/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solutionizing.net&amp;blog=3982353&amp;post=321&amp;subd=solutionizing&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solutionizing.net/2009/01/09/splist-parentweb-leaks-revisited/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/26483382e3717e58e4c45d06c8ec351d?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">dahlbyk</media:title>
		</media:content>

		<media:content url="http://solutionizing.files.wordpress.com/2009/01/splistget_securableobjectimpl.png?w=187" medium="image">
			<media:title type="html">SPList.get_SecurableObjectImpl</media:title>
		</media:content>
	</item>
		<item>
		<title>Is SPList.ParentWeb a Leak?</title>
		<link>http://solutionizing.net/2008/08/10/is-splistparentweb-a-leak/</link>
		<comments>http://solutionizing.net/2008/08/10/is-splistparentweb-a-leak/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 05:36:54 +0000</pubDate>
		<dc:creator>Keith Dahlby</dc:creator>
				<category><![CDATA[Object Model]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[BreakRoleInheritance]]></category>
		<category><![CDATA[ListViewWebPart]]></category>
		<category><![CDATA[ParentWeb]]></category>
		<category><![CDATA[SPList]]></category>
		<category><![CDATA[SPWeb]]></category>

		<guid isPermaLink="false">http://solutionizing.wordpress.com/?p=92</guid>
		<description><![CDATA[As previously discussed, doing the &#8220;right thing&#8221; with our SPSite and SPWeb references&#8217; disposability is way harder than it should be. And even worse is the fact that the experts don&#8217;t seem to agree how that should be done. Now I&#8217;m definitely not an expert, and I&#8217;m not even sure who&#8217;s moderating the debate, but [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solutionizing.net&amp;blog=3982353&amp;post=92&amp;subd=solutionizing&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As <a title="Disposing list’s SPSite/SPWeb without ParentWeb" href="http://solutionizing.net/2008/06/15/disposing-lists-spsitespweb-without-parentweb/">previously</a> <a title="SPSite/SPWeb Leaks Revisited" href="http://solutionizing.net/2008/07/23/spsite-spweb-leaks-revisited/">discussed</a>, doing the &#8220;right thing&#8221; with our <code>SPSite</code> and <code>SPWeb</code> references&#8217; disposability is way harder than it should be. And even worse is the fact that the experts don&#8217;t seem to agree how that should be done. Now I&#8217;m definitely not an expert, and I&#8217;m not even sure who&#8217;s moderating <a title="Adding a List View Web Part to a Page, Comment by Gary Lapointe" href="http://stsadm.blogspot.com/2008/08/adding-list-view-web-part-to-page.html?showComment=1218203400000#c6759731902555935766">the debate</a>, but I&#8217;m intrigued so I guess I&#8217;ll invite myself anyway.</p>
<p>First, the code in question, from a <a title="Adding a List View Web Part to a Page" href="http://stsadm.blogspot.com/2008/08/adding-list-view-web-part-to-page.html">useful STSADM extension</a> by <a title="STSADM Custom Extensions" href="http://stsadm.blogspot.com/">Gary Lapointe</a>:</p>
<pre>  lvw = new ListViewWebPart();
  SPList list = Utilities.GetListFromViewUrl(listUrl);
  if (list == null)
    throw new ArgumentException("List not found.");

  lvw.ListName = list.ID.ToString("B").ToUpperInvariant();
  lvw.TitleUrl = list.DefaultViewUrl;
  lvw.WebId = list.ParentWeb.ID;</pre>
<p>The question: Does <code>list.ParentWeb</code> need to be disposed? My first instinct was that it does, based on this quote from Roger Lamb&#8217;s <a title="SharePoint 2007 and WSS 3.0 Dispose Patterns by Example" href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx">dispose patterns</a>:</p>
<blockquote><p>When using the <strong><a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.splist.breakroleinheritance.aspx" target="_blank">SPList.BreakRoleInheritance()</a></strong> method a internal call to <strong>ParentWeb</strong> property is called and must be disposed by the caller.</p></blockquote>
<p>But upon further consideration, is that even true? Well, let&#8217;s reflect <code>SPList.ParentWeb</code> to see what we&#8217;re dealing with:</p>
<pre>    if (!SPUtility.StsCompareStrings(this.m_Lists.Web.ServerRelativeUrl, this.ParentWebUrl)) {
      if (this.m_parentWeb == null) {
        this.m_parentWeb = this.m_Lists.Web.Site.OpenWeb(this.ParentWebUrl);
      }
    }
    else {
      return this.m_Lists.Web;
    }
    return this.m_parentWeb;</pre>
<p>Or rewritten for clarity:</p>
<pre>    if (SPUtility.StsCompareStrings(this.m_Lists.Web.ServerRelativeUrl, this.ParentWebUrl))
        return this.m_Lists.Web;

    if (this.m_parentWeb == null)
        this.m_parentWeb = this.m_Lists.Web.Site.OpenWeb(this.ParentWebUrl);
    return this.m_parentWeb;</pre>
<p>So if the list&#8217;s parent collection matches its <code>ParentWebUrl</code>, it uses the collection&#8217;s <code>Web</code>; if not it returns <code>m_parentWeb</code>, which Reflector verifies is only set by the preceding call to <code>Site.OpenWeb()</code>. As far as I can tell, every call to <code>OpenWeb</code> requires a matching call to <code>Dispose</code>, and as <code>SPList</code> isn&#8217;t <code>IDisposable</code> that responsibility would seem to fall to the developer. So we should <code>Dispose</code>, right? Well, maybe&#8230; We can&#8217;t forget about the first <code>if</code>: <code>OpenWeb</code> is only called if the list isn&#8217;t in its collection&#8217;s web. (Bonus question: how could that happen?)</p>
<p>So it seems the most common scenario is that <code>SPList.ParentWeb</code> will just defer to <code>SPListCollection.Web</code>, which returns <code>SPListCollection.m_web</code>. And <code>m_web</code> is only set by an internal <code>SPListCollection</code> constructor, used by <code>SPWeb.Lists</code>:</p>
<pre>    if (this.m_Lists == null)
      this.m_Lists = new SPListCollection(this);
    return this.m_Lists;</pre>
<p>So with all that in mind, consider the following code:</p>
<pre>    SPWeb web = SPContext.Current.Web;
    SPList list = web.Lists["ListName"];
    list.BreakRoleInheritance();
    list.ParentWeb.Dispose(); // Best practice?</pre>
<p><code>BreakRoleInheritance</code> does indeed have an internal reference to <code>ParentWeb</code> (hidden in <code>SPList.SecurableObjectImpl</code>), but in this case <code>ParentWeb</code> will be <code>web</code>, which is our context web and <strong>should not</strong> be disposed!</p>
<p>So back to Gary&#8217;s example. Before we can decide how to handle <code>list.ParentWeb</code>, we should check out the helper that created list for us:</p>
<pre>    internal static SPList GetListFromViewUrl(string url) {
      using (SPSite site = new SPSite(url))
      using (SPWeb web = site.OpenWeb()) {
        return GetListFromViewUrl(web, url);
      }
    }</pre>
<p>Since the <code>SPWeb</code> was opened based on the list URL, it should be safe to assume that <code>ParentWeb</code> won&#8217;t need to call <code>OpenWeb</code> again and will instead use <code>Lists.Web</code>, which will be a reference to the original <code>web</code>&#8230;<strong>which is disposed (by using) in the helper. </strong>So <code>list.ParentWeb</code> indeed doesn&#8217;t need to be disposed because it&#8217;s <em>already been disposed</em>. In practice we can probably get away with it (in this case), but there are no guarantees that a disposed <code>SPWeb</code> will be in a consistent state when we get around to using it.</p>
<p>Though we&#8217;ve answered the original question of disposal, we should really eliminate the post-disposal reference. My preference is to refactor the <code>ListViewWebPart</code> code into helpers modeled after Gary&#8217;s existing <code>GetListFromViewUrl</code> methods:</p>
<pre>    internal static ListViewWebPart GetListViewWebPartFromViewUrl(string url) {
      using (SPSite site = new SPSite(url))
      using (SPWeb web = site.OpenWeb()) {
        return GetListViewWebPartFromViewUrl(web, url);
      }
    }

    internal static ListViewWebPart GetListViewWebPartFromViewUrl(SPWeb web, string url) {
      ListViewWebPart lvw;
      lvw = new ListViewWebPart();
      SPList list = GetListFromViewUrl(web, url);
      if (list == null)
        throw new ArgumentException("List not found.");

      lvw.ListName = list.ID.ToString("B").ToUpperInvariant();
      lvw.TitleUrl = list.DefaultViewUrl;
      lvw.WebId = web.ID;
      return lvw;
    }</pre>
<p>Which greatly simplifies the original code and eliminates the <code>ParentWeb</code> risk:</p>
<pre>  lvw = Utilities.GetListViewWebPartFromViewUrl(listUrl);</pre>
<h3>Conclusions</h3>
<p>So back again to the original question: Does <code>SPList.ParentWeb</code> need to be disposed? The answer seems to be a qualified &#8220;probably not&#8221;. But even if we&#8217;re not leaking an <code>SPWeb</code> reference, more concerning is the potential for <code>ParentWeb</code> to return an object that has already been disposed. If nothing else, be careful with <code>ParentWeb</code> on lists returned from helpers that might have cleaned up prematurely.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/solutionizing.wordpress.com/92/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/solutionizing.wordpress.com/92/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solutionizing.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solutionizing.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solutionizing.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solutionizing.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solutionizing.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solutionizing.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solutionizing.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solutionizing.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solutionizing.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solutionizing.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solutionizing.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solutionizing.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solutionizing.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solutionizing.wordpress.com/92/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solutionizing.net&amp;blog=3982353&amp;post=92&amp;subd=solutionizing&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solutionizing.net/2008/08/10/is-splistparentweb-a-leak/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/26483382e3717e58e4c45d06c8ec351d?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">dahlbyk</media:title>
		</media:content>
	</item>
		<item>
		<title>Disposing list&#8217;s SPSite/SPWeb without ParentWeb</title>
		<link>http://solutionizing.net/2008/06/15/disposing-lists-spsitespweb-without-parentweb/</link>
		<comments>http://solutionizing.net/2008/06/15/disposing-lists-spsitespweb-without-parentweb/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 03:01:13 +0000</pubDate>
		<dc:creator>Keith Dahlby</dc:creator>
				<category><![CDATA[Object Model]]></category>
		<category><![CDATA[dispose]]></category>
		<category><![CDATA[ParentWeb]]></category>

		<guid isPermaLink="false">http://solutionizing.wordpress.com/?p=7</guid>
		<description><![CDATA[Update 12/10/2008: Don&#8217;t use this technique; use the guidance here instead: The New Definitive SPSite/SPWeb Disposal Article Chris O&#8217;Brien&#8217;s recent post discusses the need to dispose of SPSite and SPWeb objects, but only if they didn&#8217;t come from SPContext. To do this he depends on a list&#8217;s ParentWeb property, which currently returns the same instance [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solutionizing.net&amp;blog=3982353&amp;post=7&amp;subd=solutionizing&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Update 12/10/2008:</strong> Don&#8217;t use this technique; use the guidance here instead: <cite></cite><a class="url" rel="external nofollow" href="http://solutionizing.net/2008/12/06/the-new-definitive-spsitespweb-disposal-article/">The New Definitive SPSite/SPWeb Disposal Article</a></p>
<p>Chris O&#8217;Brien&#8217;s <a title="Disposing SharePoint objects - what they don't tell you" href="http://www.sharepointnutsandbolts.com/2008/06/disposing-sharepoint-objects-what-they.html">recent post</a> discusses the need to dispose of SPSite and SPWeb objects, but only if they didn&#8217;t come from SPContext. To do this he depends on a list&#8217;s ParentWeb property, which currently returns the same instance as the original SPWeb. However, he notes that this strategy depends on the internal implementation of ParentWeb, which (however unlikely) might change.</p>
<p>As a future-proof alternative, I propose capturing the SPWeb as an <code>out</code> parameter if it will need to be disposed:</p>
<pre>public void DoSomething() {
  SPWeb webToDispose = null;
  SPList list = getList(out webToDispose);

  // do something with list..
  foreach (SPListItem item in list.Items) {
    processItem(item);
  }

  // ** PROBLEM - how do we now dispose of the SPSite/SPWeb objects we created earlier? **
  // ** SOLUTION - if we didn't use context, webToDispose has reference
  if (webToDispose != null) {
    webToDispose.Dispose();
    webToDispose.Site.Dispose();
  }
}

private SPList getList(out SPWeb webToDispose) {
  webToDispose = null;

  SPContext currentContext = SPContext.Current;
  SPList list = null;

  if (currentContext != null) {
    list = currentContext.Site.AllWebs["MyWeb"].Lists["MyList"];
  }
  else {
    SPSite site = new SPSite("http://solutionizing.net");
    webToDispose = site.OpenWeb("/MyWeb");
    list = webToDispose.Lists["MyList"];
  }

  return list;
}</pre>
<p>This code should be functionally equivalent and perhaps a bit easier to read, but without the dependency on ParentWeb. Thoughts?</p>
<p>Also, to add to Chris&#8217;s list of required reading, check out Roger Lamb&#8217;s excellent <a href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx">SharePoint 2007 and WSS 3.0 Dispose Patterns by Example</a>.</p>
<p><strong>Update 7/23/2008:</strong> How ironic&#8230;Chris and I have a leak! If you can&#8217;t spot it, I explain <a title="SPSite/SPWeb Leaks Revisited" href="http://solutionizing.net/2008/07/23/spsite-spweb-leaks-revisited/">here</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/solutionizing.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/solutionizing.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solutionizing.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solutionizing.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solutionizing.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solutionizing.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solutionizing.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solutionizing.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solutionizing.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solutionizing.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solutionizing.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solutionizing.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solutionizing.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solutionizing.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solutionizing.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solutionizing.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solutionizing.net&amp;blog=3982353&amp;post=7&amp;subd=solutionizing&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solutionizing.net/2008/06/15/disposing-lists-spsitespweb-without-parentweb/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/26483382e3717e58e4c45d06c8ec351d?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">dahlbyk</media:title>
		</media:content>
	</item>
	</channel>
</rss>
