Ask a questionAsk a question
 

AnswerDispose SPWeb object inside Feature Receiver? Or don't?

  • Monday, September 08, 2008 7:22 PMPSeale Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    All,

    Is there an authoritative source of information telling us whether we should dispose SPWeb objects retrieved inside a feature receiver, specifically something like:

    public override void FeatureActivated(SPFeatureReceiverProperties properties) 
        //should this be in a using block? 
        SPWeb web = properties.Feature.Parent as SPWeb; 

    I have my opinion, and I'm sure you all have your opinion, but...does anyone really know?

    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:
    http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx

Answers

  • Monday, September 08, 2008 8:18 PMTony Testa MVPMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I just looked at some old event handlers i wrote and I always cleaned up the SPWeb object.

    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.

    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.

All Replies

  • Monday, September 08, 2008 7:46 PMRich Finn [WW] Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.

    -Rich
    Rich Finn - http://blog.richfinn.net
  • Monday, September 08, 2008 8:18 PMTony Testa MVPMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I just looked at some old event handlers i wrote and I always cleaned up the SPWeb object.

    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.

    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.
  • Monday, September 08, 2008 8:25 PMMichael Washam - MSFTMSFT, ModeradorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Tony is correct. Do not dispose properties.Feature.Parent..

    However, if you use .ParentWeb or anything else the same rules apply..

    P.S. I'll let Roger know he missed one :)

    Posting is provided "AS IS" with no warranties, and confers no rights.
  • Monday, September 08, 2008 10:00 PMPSeale Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks all
  • Tuesday, September 09, 2008 12:59 AMJeremy Thake Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    For others finding this post, there is also a full set of best practices around Disposing the SharePoint objects here:

    http://msdn.microsoft.com/en-us/library/aa973248.aspx

    Cheers,
    Jeremy Thake

    SharePoint Solution Architect, Perth Australia - http://wss.made4the.net/
    • Edited byJeremy Thake Tuesday, September 09, 2008 1:00 AMhyperlink
    •  
  • Friday, May 15, 2009 6:12 PMdereck doe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Microsoft has also published a couple of articles about coding best practices for SharePoint, including disposing of SharePoint objects. 

    You can find the articles here: 


    -  http://msdn.microsoft.com/en-us/library/bb687949 .aspx 


    -  http://msdn.microsoft.com/en-us/library/aa973248 .aspx

     
    I hope this will be helpful .