0

I have updated my code as per suggestions from suggestions on my earlier question , I still see the error message for one of my recursive function.

Below is the code snippet to recursively loop through all Webs/Sub-Webs:-

        private void IterateLists(SPWeb web, ArrayList _lists)
    { 
        //Add information ablout all the lists and Last Modified dates
        foreach (SPList thisList in web.Lists)
        {
            if (!(thisList.Hidden) && (thisList.Title != _userInformationList))
            {
                //Add the list information onto the Lists collection
                _lists.Add(new ListInfo { WebUrl = web.Url, ListName = thisList.Title, LastModified = thisList.LastItemModifiedDate.ToLocalTime(), TimeDelta = SPUtility.TimeDeltaAsString(thisList.LastItemModifiedDate.ToLocalTime(), DateTime.Now.ToLocalTime()), });
            }

            //Call this method recursively if this web has subwebs
            foreach (SPWeb subWeb in web.Webs)
            {
                try
                {
                    IterateLists(subWeb, _lists);
                }
                finally
                {
                    if (subWeb != null)
                        subWeb.Dispose();
                }
            }
        }
    }

it has the code to dispose the subWeb object in the finally block. I have a huge SharePoint web application with many sites and sub sites. I see following error for this particular method.

Potentially excessive number of SPRequest objects (2859) currently unreleased on thread 9. Ensure that this object or its parent (such as an SPWeb or SPSite) is being properly disposed. This object is holding on to a separate native heap. Allocation Id for this object: {9379E575-1358-4D65-B798-7B81EE167112} Stack trace of current allocation:

1 Answer 1

1

Why are you iterating over each web for each list? shouldn't it be

private void IterateLists(SPWeb web, ArrayList _lists)
{ 
    //Add information ablout all the lists and Last Modified dates
    foreach (SPList thisList in web.Lists)
    {
        if (!(thisList.Hidden) && (thisList.Title != _userInformationList))
        {
            //Add the list information onto the Lists collection
            _lists.Add(new ListInfo { WebUrl = web.Url, ListName = thisList.Title, LastModified = thisList.LastItemModifiedDate.ToLocalTime(), TimeDelta = SPUtility.TimeDeltaAsString(thisList.LastItemModifiedDate.ToLocalTime(), DateTime.Now.ToLocalTime()), });
        }
    }
    //Call this method recursively if this web has subwebs
    foreach (SPWeb subWeb in web.Webs)
    {
        try
        {
            IterateLists(subWeb, _lists);
        }
        catch(Expection e){}
    }
    web.Dispose();
}

Otherwise you are going through each sub site for each of the lists, not all of the list in the site, then all of its subsites.

And as reflected in the above, you should do dispose in the function at the level of the current web, not in the foreach loop. Web will never be null if it was in the webs collection and thus had the function called on it.

You could do if(web != null) around the whole block if you were worried about it.

1
  • That's a good catch Thantos, I just noticed what a horrible mistake I was making...Thanks a lot, I just forgot to do a second review of my code. Thanks a ton. Commented Sep 20, 2012 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.