Any SharePoint developer with about a days worth of SharePoint development knowledge tends to find out that you need to dispose of the SPWeb and SPSite objects.  Of course this is further conveluded by the fact that this isn't ALWAYS the case depending on whether your a webpart or not, context, etc. etc.

I was answering a few forum postings this evening when someone asked the question again, whether or not they should dispose something in a certain case. I think I sufficiently answered their post on the MSDN forum.

I thought that I would share the info again though on my site so that I can easily link to it later.

Roger Lambs SPWeb/SPSite dispose write up
Roger Lambs SPWeb/SPSite dispose when using try/finally blocks
Steve Gossner's SPWeb/SPSite dispose write up has some good tidbits about other objects that need disposing
MSDN's SPWeb/SPSite dispose write up

In addition MS is coming out with a tool that is basically like FxCop that will check for SPWeb/SPSite objects, but its not out yet. MS SharePoint Team Blog  word on the street is that its coming out very soon.

The tricky thing that most of the articles don't really discuss that much, is when you use statements like"item.ParentList.ParentWeb.RoleDefinitions" that it technically creates an SPWeb object in your method when you call the ParentWeb. You therefore should probably break that line out to something like


using(SPWeb parent = item.ParentList.ParentWeb)

{

//your code here

}


Comments


January 23. 2009 03:24
Keith Dahlby
I posted a complete response in the forum, but part of it is worth repeating...

If you need to use SPList.ParentWeb, take a look at my analysis here: solutionizing.net/.../. In most cases, ParentWeb will not open a new SPWeb reference. You can test this by comparing list.ParentWeb with list.Lists.Web (which will only ever return a reference to the SPWeb from which it was created): only if they are different was a new SPWeb created that needs to be disposed.

If you're not sure, you should always err on the side of not disposing. It's usually easier to observe memory pressure (and use Stefan's troubleshooting techniques: blogs.technet.com/.../...ss-v3-and-moss-2007.aspx) than it is to track down issues related to the use of an SPWeb that was disposed too early.

Cheers ~
Keith

http://solutionizing.net/http://solutionizing.net/


January 26. 2009 20:26
GIS Inventory Management software
thanks for sharing your solution and method.

http://inventorymanagementsoftwarezone.com/http://inventorymanagementsoftwarezone.com/


January 27. 2009 17:38
SEO Dubai
Ill go for not to dispose..Smile

http://www.seodubai.org/http://www.seodubai.org/


January 29. 2009 15:13
Jeremy Thake
Keith and others have contributed to the SharePointDevWiki.com pages also which is well worth a look!
<a href="www.sharepointdevwiki.com/.../When+to+Dispose+SharePoint+objects">http://www.sharepointdevwiki.com/.../a>

http://wss.made4the.net/http://wss.made4the.net/


February 1. 2009 17:15
Inventory Management Software
hehe.. i will go to not dispose .. Smile

http://free-inventory-management-software.blogspot.com/http://free-inventory-management-software.blogspot.com/


February 1. 2009 17:15
Sports Toto
yeah.. same goes to me Tong

http://magnum4d.blogspot.com/http://magnum4d.blogspot.com/


February 2. 2009 21:28
Inventory Management Software
This mine

how-inventory-management-software.blogspot.com/http://how-inventory-management-software.blogspot.com/

http://how-inventory-management-software.blogspot.com/http://how-inventory-management-software.blogspot.com/


February 18. 2009 15:30
kitchen aid parts
let dispose it.

http://kitchen-aid-part.blogspot.com/http://kitchen-aid-part.blogspot.com/


May 10. 2009 21:24
Melayu Boleh
nice info man.. Smile

http://melayubolehinfo.blogspot.com/http://melayubolehinfo.blogspot.com/

Search Blog

Blog Roll

    OPMLDownload OPML file

    Recent Comments

    Banners

    Theme Grabber
    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010 Tony Testa's World