I have a couple of utility functions that help with querying lists and wanted to see if they I wrote them would cause either memory leaks or cause the client code to try to access already disposed objects.

 public static SPListItemCollection GetListItems(string RelativeURL)

        SPList list;

        int length = RelativeURL.LastIndexOf("/", StringComparison.OrdinalIgnoreCase);
        string webUrl = RelativeURL.Substring(0, length);
        string listName = RelativeURL.Substring(length + 1);

        using (SPWeb web = SPContext.Current.Site.OpenWeb(webUrl))
            list = web.Lists[listName];


        return list.Items;


    public static SPListItemCollection GetListItems(string RelativeURL, string CamlQuery)
        SPQuery query = new SPQuery();
        query = new SPQuery();
        query.Query = CamlQuery;
        return GetListItems(RelativeURL, query);


The client code would look like this:

SPListItemCollection items = GetListItems("/subsite/mylist");


SPListItemCollection items = GetListItems("/subsite/mylist", "<where>...");

You're disposing an instance of SPWeb got through using SPContext. This probably will result in unpredictable behaviour as something else is expecting to dispose of the Site object you've got it from.

Rather, use new SPSite(SPContext.Current.Site.Url) and wrap that in a using tag like you've done there.

EDIT: Yeah, you're using OpenWeb() which does indeed return a new SPWeb, and you're disposing of that, but it's just risky to use a property or method that a class returns a new IDisposable of, as you don't know if the parent object keeps track of it's 'child' objects upon attempting to Dispose of them when the parent itself is Disposed.

  • OpenWeb() creates a new SPWeb object in memory and it should be disposed...even if using SPContext.Current.Site.OpenWeb() – Amit Kumawat Nov 1 '11 at 10:36

There is no memory leak in your code for SPWeb. The SPWeb object you get from SPContext.Current.Site.OpenWeb(webUrl) should be disposed (like you already did).

There is are other performance improvements I can suggest in your code:

1) Use list = web.GetList(lstUrl) instead of list = web.Lists[listName] ;

2) Use list.GetItems([SPQuery]) instead of list.Items. You should fetch only the columns and items which you need. (SPList.Items return a lot of internal columns which you may not need).

3) If your list is too large(>5000 items), You should use ContentIterator class to avoid hitting throttle limits (By default, this limit is 5,000 items for regular users and 20,000 items for administrators)

  • Thanks for you input. The majority of the lists we are querying are less then the 2000 and usually involve a filter using caml. I will take a look at the web.GetList(listUrl) method. I was planning on adding a way to list out just the columns you need as well so we don't keep returning all unneeded columns. – David Nov 2 '11 at 12:54

The number one memory leak cause are not disposed SPWeb and SPSite objects.

Microsoft has released a tool that will do the check for you. Check out SharePoint Dispose Checker Tool

This MS Tool will also integrate into Visual Studio as a plug-in and will check on every build.

  • The SPDispose tool does not flag this as an issue, but one of our consultant developers says its causing issues when we moved from 2007 to 2010. – David Oct 31 '11 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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