| Just about any developer working long enough with | | | | of the SPWeb object. |
| SharePoint will, at some point, wake up in the middle | | | | Other updates I found interesting (and frustrating) |
| of the night with the words "Memory Leak" echoing | | | | are: |
| in through their head... No - it is not the cat's fault, | | | | * Calling SPWeb.AllWebs collection requires you to |
| and no it does not mean you forgot to take out the | | | | dispose of this collection object. |
| trash again... What did happen? You came across one | | | | * Creating a new site by SPWeb.Webs.Add() - need |
| of Microsoft's "best practices" articles regarding | | | | to dispose of the web object returned. |
| disposing of SPWeb and SPSite objects in | | | | * Calling GetLimitedWebPartManager() method? It will |
| SharePoint... | | | | create a SPWeb object of its own and not dispose |
| According to the original MS guidance, every SPWeb | | | | of it properly. |
| or SPSite object you create (or get) that was not | | | | Please review the complete updated best practices |
| initiated within the page's context (i.e. not | | | | guide at MSDN here: |
| SPContext.Current.Web and not | | | | I hope Microsoft will release an update for this issue |
| SPControl.GetContextWeb(Context) ) must be | | | | soon.. Just thinking of all the code lines I wrote under |
| disposed of. | | | | the wrong best practices article is making me dizzy... |
| It used to be that the "using" statement construct | | | | Good luck and happy disposing! |
| was recommended to avoid forgetting to dispose... in | | | | Shai Petel. |
| other cases you'd put up a flag and dispose of it at | | | | =~=~= Update =~=~= |
| the end of your code's life cycle - either way was | | | | Ok, this explains why it took me so long to find this |
| good enough. | | | | problem... |
| Being the "good guys" that we are, we went over | | | | I had this code in my web part, written according to |
| and updated all of our codes and components to | | | | the original best practices:using(SPSite site = |
| match these guide lines. | | | | SPContext.Current.Web.Site) |
| And what happened? Every once in a while disposing | | | | { |
| these objects would cause baffling errors such as: | | | | using(SPWeb web = site.RootWeb) |
| "Trying to use a SPWeb object that has been closed | | | | { |
| or disposed and is no longer valid". | | | | //Some code here... |
| But hey, if we call SPContext.Current.Site.RootWeb - | | | | } |
| the best practices guidelines said we have to dispose | | | | } |
| of it! Same goes for SPContext.Current.Web.Site and | | | | I cannot explain this but I only got this error on some |
| SPContext.Current.Web.Site.RootWeb etc. | | | | sites, and on others it worked OK. Strange huh? |
| Recently I came across a great article by Roger | | | | What I found is that this code will throw exception |
| Lamb showing a few updates from Microsoft | | | | on any team site I created within a site collection |
| regarding these disposable objects and | | | | that is not in the web application root path (i.e. sites |
| recommendations. | | | | under |
| So - pay close attention to these updates... it just | | | | Anyway - according to the new guidelines, I should |
| might help you sleep through the night! | | | | not dispose of the RootWeb object in this case. If I |
| * When making a call to | | | | dispose of the SPSite object - it disposes of the |
| SPContext.Current.Web.Site.RootWeb - you now | | | | RootWeb object -in this case my context web |
| have to dispose only of the SPSite object: | | | | object... |
| SPContext.Current.Web.Site! | | | | So I updated my code to something like this and it |
| Basically, you should NOT dispose of the | | | | worked: |
| SPSite.RootWeb object directly. | | | | SPWeb web = SPContext.Current.Site.RootWeb;//No |
| * When calling SPWeb.ParentWeb - you should NOT | | | | disposing |
| dispose of the ParentWeb object! | | | | //Some code here... |
| * Same goes to SPList.ParentWeb - do NOT dispose | | | | |