Dispose SPWeb object inside Feature Receiver? Or don't?All,<br><br>Is there an authoritative source of information telling us whether we should dispose SPWeb objects retrieved inside a feature receiver, specifically something like:<br><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px"></font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">void</font><font style="font-size:11px"> FeatureActivated(SPFeatureReceiverProperties properties) </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">{ </td></tr><tr><td>    <font style="color:green">//should this be in a using block?</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    SPWeb web = properties.Feature.Parent <font style="color:blue">as</font><font style="font-size:11px"> SPWeb; </font></td></tr><tr><td>} </td></tr></tbody></table></div><br>I have my opinion, and I'm sure you all have your opinion, but...does anyone really know?<br><br>Also before you start, I'll pre-empt you and link to Roger Lamb's post on the subject. He doesn't cover Feature Receivers specifically:<br><a href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx">http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx</a><br> © 2009 Microsoft Corporation. All rights reserved.Fri, 15 May 2009 18:12:36 Zc0f1a05f-f1db-4f37-8f24-5966ab43e73bhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#c0f1a05f-f1db-4f37-8f24-5966ab43e73bhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#c0f1a05f-f1db-4f37-8f24-5966ab43e73bPSealehttp://social.msdn.microsoft.com/Profile/en-US/?user=PSealeDispose SPWeb object inside Feature Receiver? Or don't?All,<br><br>Is there an authoritative source of information telling us whether we should dispose SPWeb objects retrieved inside a feature receiver, specifically something like:<br><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px"></font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">void</font><font style="font-size:11px"> FeatureActivated(SPFeatureReceiverProperties properties) </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">{ </td></tr><tr><td>    <font style="color:green">//should this be in a using block?</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">    SPWeb web = properties.Feature.Parent <font style="color:blue">as</font><font style="font-size:11px"> SPWeb; </font></td></tr><tr><td>} </td></tr></tbody></table></div><br>I have my opinion, and I'm sure you all have your opinion, but...does anyone really know?<br><br>Also before you start, I'll pre-empt you and link to Roger Lamb's post on the subject. He doesn't cover Feature Receivers specifically:<br><a href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx">http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx</a><br> Mon, 08 Sep 2008 19:22:50 Z2008-09-08T19:22:50Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#c9a0d536-d5de-49a8-88f7-843d8ff44e65http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#c9a0d536-d5de-49a8-88f7-843d8ff44e65Rich Finn [WW]http://social.msdn.microsoft.com/Profile/en-US/?user=Rich%20Finn%20%5bWW%5dDispose SPWeb object inside Feature Receiver? Or don't? I know I normally don't dispose of the Feature.Parent, but would be a good thing to be certain on.  My thought was that it's kinda similar to the Feature context, so don't dispose.<br><br>-Rich<hr size="1" align="left" width="25%">Rich Finn - http://blog.richfinn.netMon, 08 Sep 2008 19:46:42 Z2008-09-08T19:46:42Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#e6ee5b39-fe25-4ce1-beee-006259431229http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#e6ee5b39-fe25-4ce1-beee-006259431229Tony Testa MVPhttp://social.msdn.microsoft.com/Profile/en-US/?user=Tony%20Testa%20MVPDispose SPWeb object inside Feature Receiver? Or don't? I just looked at some old event handlers i wrote and I always cleaned up the SPWeb object.<br><br>BUT if your using properties.feature.parent, you would NOT want to kill the object because you would be killing the object that is running your code, same as in a webpart, you won't want to kill the SPContext.Current.Web object because its running your webpart.<br><br>Typically in my event handlers I need to run code elevated, so i create the SPWeb or SPSite objects inside the elevated code and use the ID or URL from the properties collection to create the objects, that way I am creating new objects and can dispose of them knowing that I am not killing the object that is running my code.Mon, 08 Sep 2008 20:18:40 Z2008-09-08T20:18:40Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#005d522e-ca8e-482b-bd7a-145ef454bf55http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#005d522e-ca8e-482b-bd7a-145ef454bf55Michael Washam - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Michael%20Washam%20-%20MSFTDispose SPWeb object inside Feature Receiver? Or don't? Tony is correct. Do not dispose properties.Feature.Parent..<br><br>However, if you use .ParentWeb or anything else the same rules apply..<br><br>P.S. I'll let Roger know he missed one :)<br><hr size="1" align="left" width="25%">Posting is provided &quot;AS IS&quot; with no warranties, and confers no rights.Mon, 08 Sep 2008 20:25:20 Z2008-09-08T20:25:20Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#5cac3339-120d-451d-b0ec-907e3330cafbhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#5cac3339-120d-451d-b0ec-907e3330cafbPSealehttp://social.msdn.microsoft.com/Profile/en-US/?user=PSealeDispose SPWeb object inside Feature Receiver? Or don't?Thanks all Mon, 08 Sep 2008 22:00:44 Z2008-09-08T22:00:44Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#403fe602-767a-479c-aadb-1e026d4a89fchttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#403fe602-767a-479c-aadb-1e026d4a89fcJeremy Thakehttp://social.msdn.microsoft.com/Profile/en-US/?user=Jeremy%20ThakeDispose SPWeb object inside Feature Receiver? Or don't?For others finding this post, there is also a full set of best practices around Disposing the SharePoint objects here:<br><br><a href="http://msdn.microsoft.com/en-us/library/aa973248.aspx">http://msdn.microsoft.com/en-us/library/aa973248.aspx</a><br><br>Cheers,<br>Jeremy Thake<br> <hr size=1 width="25%" align=left>SharePoint Solution Architect, Perth Australia - <a href="http://wss.made4the.net/">http://wss.made4the.net/</a>Tue, 09 Sep 2008 00:59:02 Z2008-09-09T01:00:17Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#e19d3efe-1132-47ed-99fe-aee8cb011fa8http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c0f1a05f-f1db-4f37-8f24-5966ab43e73b#e19d3efe-1132-47ed-99fe-aee8cb011fa8dereck doehttp://social.msdn.microsoft.com/Profile/en-US/?user=dereck%20doeDispose SPWeb object inside Feature Receiver? Or don't?<span class=Apple-style-span style="border-collapse:collapse;color:#000000;font-family:Arial;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Microsoft has also published a couple of articles about coding best practices for SharePoint, including disposing of SharePoint objects.<span class=Apple-converted-space> </span> <br/> <br/> You can find the articles here:<span class=Apple-converted-space> </span> <br/> <br/> <br/> -<span class=Apple-converted-space> </span> <a href="http://msdn.microsoft.com/en-us/library/bb687949">http://msdn.microsoft.com/en-us/library/bb687949</a> .aspx<span class=Apple-converted-space> </span> <br/> <br/> <br/> -<span class=Apple-converted-space> </span> <a href="http://msdn.microsoft.com/en-us/library/aa973248">http://msdn.microsoft.com/en-us/library/aa973248</a> .aspx<br/> <br/> <span class=Apple-converted-space> </span> <br/> </span> I hope this will be helpful <a href="http://kiwidepia.com">.</a>Fri, 15 May 2009 18:12:36 Z2009-05-15T18:12:36Z