<?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/"
	>

<channel>
	<title>Florent Clairambault &#187; Dispose</title>
	<atom:link href="http://florent.clairambault.fr/tag/dispose/feed" rel="self" type="application/rss+xml" />
	<link>http://florent.clairambault.fr</link>
	<description></description>
	<lastBuildDate>Sat, 28 Aug 2010 15:26:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
		<item>
		<title>Sharepoint : What should I Dispose ?</title>
		<link>http://florent.clairambault.fr/sharepoint-what-should-i-dispose?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=sharepoint-what-should-i-dispose</link>
		<comments>http://florent.clairambault.fr/sharepoint-what-should-i-dispose#comments</comments>
		<pubDate>Tue, 17 Feb 2009 20:10:41 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[Dispose]]></category>
		<category><![CDATA[Sharepoint]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=215</guid>
		<description><![CDATA[When you begin with Sharepoint like it did (and still do), you will ask your self : &#8220;What object should I dispose ?&#8221;. It&#8217;s quite important because Sharepoint leaks approximatively 1 Mb per IDisposable object not disposed. If it&#8217;s a menu, it could quickly make you loose 10 Mb per loaded page. The best and [...]]]></description>
			<content:encoded><![CDATA[<p>When you begin with Sharepoint like it did (and still do), you will ask your self : &#8220;What object should I dispose ?&#8221;. It&#8217;s quite important because Sharepoint leaks approximatively 1 Mb per IDisposable object not disposed. If it&#8217;s a menu, it could quickly make you loose 10 Mb per loaded page.</p>
<p>The best and complete answer is <a href="http://msdn.microsoft.com/en-us/library/aa973248.aspx">in the MSDN</a>. But it&#8217;s a pretty long answer.</p>
<p>The short answer is :<br />
In your webpart, you should dispose every SPWeb and SPSite you use except :</p>
<ul>
<li>SPContect.Current.Site</li>
<li>SPContext.Current.Web</li>
<li>SPContext.Current.Site.RootWeb</li>
</ul>
<p>In your features, you should dispose every SPWeb and SPSite you use except the ones given in your &#8220;properties&#8221; variable.</p>
<p>The reason is that Sharepoint gives these objects to every component it will launch, if you dispose one of these objects, the next component loaded by sharepoint has a good chance to crash. And it&#8217;s not always easy to debug as it&#8217;s the NEXT component which will crash also it&#8217;s the previous one that messed up one of the sharepoint context variables.</p>
<p>If your not sure that you will Dispose an object you should, you shoud check. The following code Disposes the SPWeb passed as argument and every parent except the last one.</p>

<div class="wp_codebox"><table><tr id="p2152"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p215code2"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> SPWeb SPWebParentWebGenerationDp<span style="color: #008000;">&#40;</span> SPWeb web, <span style="color: #6666cc; font-weight: bold;">int</span> generation <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> generation<span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		SPWeb toDispose <span style="color: #008000;">=</span> web<span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> web <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&#41;</span>
			web <span style="color: #008000;">=</span> web<span style="color: #008000;">.</span><span style="color: #0000FF;">ParentWeb</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> toDispose <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> toDispose <span style="color: #008000;">!=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Web</span> <span style="color: #008000;">&#41;</span>
			toDispose<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> web<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/sharepoint-what-should-i-dispose/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
