title: SharePoint Dispose SPWeb , SPSite and objects
body: <p>I need an advice on sharepoint logs related to disposing objects.</p>
  
  <p>I was looking in the logs and found these : </p>
  
  <p><strong>Unexpected Detected use of SPRequest for previously closed SPWeb object.</strong>
  Please close SPWeb objects when you are done with all objects obtained from them, but not before.</p>
  
  <pre><code>Stack trace:
      at Microsoft.SharePoint.SPFile.PropertiesCore(Boolean throwException) 
      at Microsoft.SharePoint.SPFile.get_CheckInComment() 
      ...
  </code></pre>
  
  <p><strong>Monitorable An SPRequest object was not disposed before the end of this thread.</strong> </p>
  
  <p>To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it.  This object will now be disposed.  </p>
  
  <pre><code>This SPRequest was allocated at
      at Microsoft.SharePoint.Library.SPRequest..ctor() 
      at Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(SPSite site, String name, Boolean bNotGlobalAdminCode, String strUrl, Boolean bNotAddToContext, Byte[] UserToken, String userName, Boolean bIgnoreTokenTimeout, Boolean bAsAnonymous) 
      at Microsoft.SharePoint.SPWeb.InitializeSPRequest() 
      at Microsoft.SharePoint.SPWeb.GetFileOrFolderProperties(String strUrl, ListDocsFlags listDocsFlags, Boolean throwException, SPBasePermissions&amp; permMask) 
      at Microsoft.SharePoint.SPFile.PropertiesCore(Boolean throwException) 
      at Microsoft.SharePoint.SPFile.get_CheckInComment() 
      ...
  </code></pre>
  
  <p>I can't figure out what is the main difference between these two codes as i dispose objects the same way.</p>
  
  <p>What is wrong fith the For loop ?</p>
  
  <p><strong>Code which creates log error  :</strong></p>
  
  <pre><code>using (SPSite site = new SPSite(url))
  {
      using (SPWeb web = site.OpenWeb())
      {
          string serverRelativeUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, listRelativeUrl);
          SPList lst =  web.GetList(serverRelativeUrl);
  
          items = lst.Items;
  
          if (items != null)
          {
              int nb = items.Count;
              for (int i = nb - 1; i &gt; -1; i--)
              {
                  currItem =items[i];
  
                  if (currItem.File.CheckInComment.Equals(comment1) || currItem.File.CheckInComment.Equals(comment2))
                  {
                      publish = true;
                  }
  
                  if(publish)
                  {
                      //do stuff with currItem data
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Code which does not create log error :</strong> </p>
  
  <pre><code>    using (SPSite site = new SPSite(url))
  {
      using (SPWeb web = site.OpenWeb())
      {
          string serverRelativeUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, listRelativeUrl);
          SPList lst =  web.GetList(serverRelativeUrl);
  
          items = lst.Items;
  
          if (items != null)
          {
              foreach(SPListItem currItem in items )
              {           
                  if (currItem.File.CheckInComment.Equals(comment1) || currItem.File.CheckInComment.Equals(comment2))
                  {
                      publish = true;
                  }
  
                  if(publish)
                  {
                      //do stuff with currItem data
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I will need do use the For loop because i wil need to delete item in the enumeration, thing i won't be able to do with the foreach loop.</p>
  
  <p>If you have any idea to help me please. </p>
  
  <p>Thanks.</p>