运行SPDisposeChecker工具时出错

时间:2012-02-28 23:55:17

标签: sharepoint sharepoint-2010

我收到错误“Disposable type not disposed Microsoft.SharePoint.SPWeb ***这可能是误报,具体取决于类型的创建方式或是否在当前范围之外”。

以下是我的代码:

 public static int AddtoList( string title)
    {
        int returnValue = int.MinValue;
        SPUser sysAcount = SPContext.Current.Web.AllUsers[@"SHAREPOINT\SYSTEM"];
        SPUserToken sysAcountToken = sysAcount.UserToken;
        using (SPSite siteCollection = new SPSite(SPContext.Current.Site.Url, sysAcountToken))
        {
                SPWeb currentWeb = siteCollection.RootWeb;
                SPList list = currentWeb.Lists[MyList];
                SPListItem newItem = errorList.Items.Add();  
                newItem[TitleColumnName] = title;
                currentWeb.AllowUnsafeUpdates = true;
                newItem.SystemUpdate(false);
                currentWeb.AllowUnsafeUpdates = false;
                returnValue = newItem.ID;

        }
        return returnValue;
    }

我明白当我们使用RootWeb时,我们没有处置该对象。请让我知道此错误消息的含义以及如何更正错误消息。我有几个博客bug无法理解错误。请帮帮我。

2 个答案:

答案 0 :(得分:0)

您是否拥有最新版本的SPDisposeCheck?

旧版本(2010年12月?)错误地标记.RootWeb

http://blogs.technet.com/b/stefan_gossner/archive/2010/12/15/first-issue-with-spdisposecheck-has-been-identified-by-the-community.aspx

答案 1 :(得分:0)

请在下面的代码示例中尝试SPDisposeChecker错误解析。

     public static int AddtoList( string title) 
     {         
      int returnValue = int.MinValue; 
      SPUser sysAcount = SPContext.Current.Web.AllUsers[@"SHAREPOINT\SYSTEM"];
      SPUserToken sysAcountToken = sysAcount.UserToken;
      using (SPSite siteCollection = new SPSite(SPContext.Current.Site.Url, sysAcountToken))
     { 
       //Add below code for dispose currentWeb object end of the functionality             
       using(SPWeb currentWeb = siteCollection.RootWeb)
       {
          SPList list = currentWeb.Lists[MyList];
          SPListItem newItem = errorList.Items.Add(); 
          newItem[TitleColumnName] = title; 
          currentWeb.AllowUnsafeUpdates = true; 
          newItem.SystemUpdate(false);  
          currentWeb.AllowUnsafeUpdates = false; 
          returnValue = newItem.ID;  
        }
      }        
      return returnValue;  
   } 

快乐的SharePointing !!!

谢谢,