I need an advice on sharepoint logs related to disposing objects.
I was looking in the logs and found these :
Unexpected Detected use of SPRequest for previously closed SPWeb object. Please close SPWeb objects when you are done with all objects obtained from them, but not before.
Stack trace:
at Microsoft.SharePoint.SPFile.PropertiesCore(Boolean throwException)
at Microsoft.SharePoint.SPFile.get_CheckInComment()
...
Monitorable An SPRequest object was not disposed before the end of this thread.
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.
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& permMask)
at Microsoft.SharePoint.SPFile.PropertiesCore(Boolean throwException)
at Microsoft.SharePoint.SPFile.get_CheckInComment()
...
I can't figure out what is the main difference between these two codes as i dispose objects the same way.
What is wrong fith the For loop ?
Code which creates log error :
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 > -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 which does not create log error :
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
}
}
}
}
}
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.
If you have any idea to help me please.
Thanks.