Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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:

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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. –  Sudhir Kesharwani Sep 20 '12 at 18:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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