Reputation: 181124
I have a strange issue: I am using SPContext.Current.Web in a .aspx page, but at the end, I get a "Trying to use an SPWeb object that has been closed or disposed and is no longer valid." error message.
From what I see, SPContext.Current.Web is Disposed by someone, but I have no idea where. I just wonder: With Visual Studio 2005's Debugger, can I somehow see where/who disposed an Object? As I neither create nor have the source code, setting breakpoints is a problem.
What would be a good approach for finding out who disposes a given object where, without just randomly commenting out lines?
(Note: The Issue has been resolve, but the question itself also applies outside of Sharepoint)
Upvotes: 3
Views: 2770
Reputation: 1126
In your custom code make sure you didn't get a reference to the actual SPWeb object of the Context object and dispose of it. For example, the following is very bad.
using (SPWeb myWeb = SPContext.Current.Web)
{
// do something
}
This will cause the SPContext's object to be disposed and may not cause an issue in your code, but will likely cause issues later.
Upvotes: 0
Reputation: 97997
Check if this helps:
If a dialog appears saying that There is no source code available for the current location when the breakpoint is hit dismiss it.
Note: Because I do not have SharePoint installed I have tested this with System.IO.StreamReader.Dispose
but I am guessing that this should also work for SPContext.Current.Web. Drop a note on this.
Upvotes: 6
Reputation: 13840
You should read this: http://msdn.microsoft.com/en-us/library/aa973248.aspx
To be quick: you should dispose all your SPWeb and SPSite using either
using(SPWeb web = ...)
{
....
}
or
SPWeb web = ...
...
web.Dispose()
Upvotes: -1