I've been using the following code in a .NET 1.1 SharePoint 2003 environment and it works great:
try
{
site = SPControl.GetContextSite(Context);
web = site.OpenWeb();
...
}
catch (Exception export)
{
output.Write("Caught Exception: <br/><br/>");
output.Write(export.Message + "<br><br>");
output.Write(export.StackTrace);
}
finally
{
if (web != null)
web.Dispose();
if (site != null)
site.Dispose();
}
However, I'm currently porting the code to a .NET 2.0 SharePoint 2007 environment and I get the following error message:
"Trying to use an SPWeb object that has been closed or disposed and is no longer valid."
If I comment out the Dispose() code, it works fine. But won't this cause memory leaks? What's the best way to fix the problem?