sharepoint spweb and spsite disposal
        Posted  
        
            by user327045
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user327045
        
        
        
        Published on 2010-05-20T20:10:40Z
        Indexed on 
            2010/05/21
            0:00 UTC
        
        
        Read the original article
        Hit count: 256
        
sharepoint
|.NET
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?
© Stack Overflow or respective owner