Trying to use an SPWeb object that has been closed or disposed and is no longer valid.

Nov 17, 2009 at 4:52 PM

Excellent works great but I did run into something that it appears to have an issue with.

I have the fabulous 40 intstalled on my MOSS server and I was using the sample knowledge base template as a starting point for a knowledge base on our site.  I added a lookup filtered lookup field to the columns in the list that stores the knowledge base articles and it allows me to add and edit the articles just fine.  But when I try to view an article (wiki page) I get the following error:

"Trying to use an SPWeb object that has been closed or disposed and is no longer valid."

If I remove the column, the wiki page comes up fine and no error.  So it appears the wiki view web control is having an issue with columns using the filtered lookup.  Any ideas??

Jan 12, 2010 at 11:11 AM

I had a simular problem, and Googled on "Trying to use an SPWeb object that has been closed or disposed and is no longer valid" and i found that it is a dispose problem. It seems that the problem occurs when disposing an object that is retrieved from the context of the page/control.

If looked into the code of the FilteredLookupField and came up to:

using (SPSite site = SPControl.GetContextSite(Context)) { }

this disposes the SPSite in the Context. Appears 8 times in code. Remove the using and all works fine.

For best practices on using dispose on objects in sharepoint

May 19, 2011 at 4:36 PM

I have the same problem.

you are just removing the word 'using'?

From where specifically are you making these changes?

Nov 14, 2011 at 2:00 AM

If you remove the using statements then you should dispose of the object(s) later using dispose(). I have not reviewed this source so the following is a generic example!




    SPSite site = SPControl.GetContextSite(Context);
    // do something
catch (Exception e) 
    Console.WriteLine("Exception Message: {0}", e.Message);
    //dispose of the site object

Dec 17, 2011 at 9:01 AM
Edited Dec 17, 2011 at 9:02 AM

@panoone:  While you are correct there this is not the problem in this case.

If you look at the MSDN site for SPControl.GetContextSite ( then the Remarks section contains the following note:

Do not use the Dispose or Close method of the SPSite class to close the object returned through the GetContextSite method. Instead let SharePoint Foundation or your portal application manage the object.

This actually true for GetContextWeb as well, the reason is that you did not create the SPWeb object therefore you are not responsible for calling dispose on it, the system is.  Ordinarily if you use


 SPSite site = new SPSite("http://mysite")

You have created the object and therefore have to dispose of it once finished, so using () is the correct way, if you want the error information then altering it to do as you show would be correct expansion of using.


So all that is necessary to fix the code is find out where SPContext.GetSite or SPContext.GetWeb is used and remove the using (therefore not disposing of something you shouldn't touch).

It should be noted that is you create a disposable object off something returned by SPContext you are responsible:


 SPSite site site= SPControl.GetContext(Context);
  // if you do this then you are responsible (hence using is correct)
 using( SPWeb web = site.OpenWeb() )
    // code

Hope this clears up the problem.
Dec 20, 2011 at 9:43 PM

The primary issue I'm facing, irrespective of any memory leaks, is that this solution simply doesn't work with the current source.

Has anyone else successfully compiled, deployed and got this field type working as advertised?

Jan 19, 2012 at 10:53 AM

the thing is that it's not only about memory leaks!

Yes the disposal patterns is wrongly implemented but it does not bring memory leaks, it brings unstable SharePoint because basically it shoots itself in the foot.

So fixing this disposable issue will make things works!

Follow what the others told, it's basically what I did and It does work!

Feb 1, 2012 at 4:13 PM

I am getting this error when trying to customize a page with two of these fields on it. Can anyone point me to the file (and location) I need to edit? Thanks so much.