<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Free the SPWeb!</title><link>http://blogs.msdn.com/jannemattila/archive/2007/06/29/free-the-spweb.aspx</link><description>Every SharePoint developer knows that you need to free resources that you have used... like SPWeb (and others too!). Most often freeing up the resources are easily managed with the nice using -statement like this: 1 2 3 4 5 6 7 using ( SPSite site = new</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Free the SPWeb!</title><link>http://blogs.msdn.com/jannemattila/archive/2007/06/29/free-the-spweb.aspx#5937685</link><pubDate>Tue, 06 Nov 2007 18:07:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5937685</guid><dc:creator>Matt Collinge</dc:creator><description>&lt;p&gt;Very enlightening!! Shame the WSS developers don't follow this 'Best Practice'! I had some messages appearing in the trace log: &amp;nbsp;An SPRequest object was not disposed before the end of this thread. &amp;nbsp;To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it. &lt;/p&gt;
&lt;p&gt;Turns out that the SPListEventProperties object creates an SPWeb object but never disposes of it!&lt;/p&gt;
&lt;p&gt;It has a Property Web (courtesy of Lutz Roeder's Reflector):&lt;/p&gt;
&lt;p&gt;public SPWeb Web&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.m_web == null)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while (this.WebUrl != null)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.m_web = new SPSite(this.WebUrl).OpenWeb();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.m_web;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Shouldn't SPListEventProperties implement IDispose?&lt;/p&gt;
</description></item><item><title>re: Free the SPWeb!</title><link>http://blogs.msdn.com/jannemattila/archive/2007/06/29/free-the-spweb.aspx#6886250</link><pubDate>Fri, 28 Dec 2007 19:04:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6886250</guid><dc:creator>drudolph</dc:creator><description>&lt;p&gt;I'm getting the &amp;quot;An SPRequest object was not disposed before the end of this thread&amp;quot; message. However, it looks like the allocation spot is not in my code:&lt;/p&gt;
&lt;p&gt;This SPRequest was allocated at &amp;nbsp; &amp;nbsp;at Microsoft.SharePoint.Library.SPRequest..ctor() &amp;nbsp; &amp;nbsp; at Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(Boolean bNotGlobalAdminCode, String strUrl, Boolean bNotAddToContext, Byte[] UserToken, Boolean bIgnoreTokenTimeout, Boolean bAsAnonymous) &amp;nbsp; &amp;nbsp; at Microsoft.SharePoint.SPWeb.InitializeSPRequest() &amp;nbsp; &amp;nbsp; at Microsoft.SharePoint.SPWeb.EnsureSPRequest() &amp;nbsp; &amp;nbsp; at Microsoft.SharePoint.SPWeb.get_Request() &amp;nbsp; &amp;nbsp; at Microsoft.SharePoint.SPListItemCollection.EnsureLis...	 &lt;/p&gt;
&lt;p&gt;12/28/2007 08:23:51.65*	w3wp.exe (0x1A10) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 	0x0CCC	Windows SharePoint Services &amp;nbsp; 	General &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 	8l1n	High &amp;nbsp; &amp;nbsp;	...tItemsData() &amp;nbsp; &amp;nbsp; at Microsoft.SharePoint.SPListItemCollection.Undirty() &amp;nbsp; &amp;nbsp; at Microsoft.SharePoint.SPBaseCollection.System.Collections.IEnumerable.GetEnumerator() &amp;nbsp; &amp;nbsp; at MYCODE&amp;quot;&lt;/p&gt;
&lt;p&gt;Where MYCODE is a code block that is looping (foreach) through an SPList.Item collection, hence the GetEnumerator call SPBaseCollection is making above.&lt;/p&gt;
&lt;p&gt;I get the list whose items I am looping through from code like:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;using (SPSite site = new SPSite(siteURL))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;using (SPWeb web = site.OpenWeb())&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return web.Lists[listName];&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;Is this OK? Is there anything I can do prevent this error?&lt;/p&gt;
</description></item><item><title>re: Free the SPWeb!</title><link>http://blogs.msdn.com/jannemattila/archive/2007/06/29/free-the-spweb.aspx#7716286</link><pubDate>Fri, 15 Feb 2008 17:16:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7716286</guid><dc:creator>jeromeL</dc:creator><description>&lt;p&gt; using (SPSite site = new SPSite(siteURL))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; using (SPWeb web = site.OpenWeb())&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return web.Lists[listName];&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;this code is not good at all since your method return a splist, which is dependent of your spweb and spsite and will prevent them from being disposed, or you'll have error as the parent object has been freed from memory&lt;/p&gt;
&lt;p&gt;you have to do all you operations on your list before you can dispose the web and site objects&lt;/p&gt;
</description></item><item><title>re: Free the SPWeb!</title><link>http://blogs.msdn.com/jannemattila/archive/2007/06/29/free-the-spweb.aspx#8414590</link><pubDate>Mon, 21 Apr 2008 16:37:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8414590</guid><dc:creator>Sanjay</dc:creator><description>&lt;p&gt;This is my first comments Ilike these are all great story.&lt;/p&gt;
</description></item><item><title>re: Free the SPWeb!</title><link>http://blogs.msdn.com/jannemattila/archive/2007/06/29/free-the-spweb.aspx#8657252</link><pubDate>Thu, 26 Jun 2008 19:12:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8657252</guid><dc:creator>PD</dc:creator><description>&lt;p&gt;Nice blog! Thanks for posting it.&lt;/p&gt;
</description></item><item><title>re: Free the SPWeb!</title><link>http://blogs.msdn.com/jannemattila/archive/2007/06/29/free-the-spweb.aspx#9699355</link><pubDate>Thu, 04 Jun 2009 14:54:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9699355</guid><dc:creator>Craig</dc:creator><description>&lt;p&gt;Nice post.&lt;/p&gt;
&lt;p&gt;By far the best posting I have seen on how to correctly dispose of sharepoint objects is at:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx"&gt;http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Free the SPWeb!</title><link>http://blogs.msdn.com/jannemattila/archive/2007/06/29/free-the-spweb.aspx#9832962</link><pubDate>Tue, 14 Jul 2009 12:41:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9832962</guid><dc:creator>Tj</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I hav'nt used any code. All I have done is just creating a site and adding some web parts to it. Then I got the error message &amp;quot;Trying to use the SPWeb object that has been disposed!!!&amp;quot; &amp;nbsp;can someone explain this...&lt;/p&gt;
&lt;p&gt;Thank You.&lt;/p&gt;
</description></item></channel></rss>